thur thur - 3 months ago 11
JSON Question

How can I generate or import multiple json files in one, with webpack?

I have multiple json files, and want to transform they to one simple json file and use

json-loader
to require it, like this:

import dictionary from './distFile.json';


Example:

file1.json

{a:"b"}


file2.json

{b:"a"}

distFile.json should be:

{a:"b",b:"a"}


How can I do that?

Or there is anyway to do something like that:

import dictionary from ['./file1.json','./file2.json'];
??

Answer

Make a third file that you require both files in.

combined.js

import file1 from './file1.json';
import file2 from './file2.json';
export default { ...file1, ...file2 };

Then just import that file:

import combinedJson from './combined.js'
Comments