I'm looking for the best way to bine json files in a folder.
With HTML, CSS and JavaScript this is pretty easy since you don't need a separator or only a single ;
.
However, with JSON, we need something more to make it a valid JSON object.
One way would be to concatenate the files with ,
and wrap everything in an array. I'm wondering if there is an better/easier way to do this.
Gruntfile.js
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
json: {
src: ['src/**/*.json'],
dest: 'dist/bined.json',
options: {
...
}
}
}
});
src/file1.json
{
"number": 1
}
src/file2.json
{
"number": 2
}
dist/bined.json
This would be the desired oute:
{
"numbers": [
{
"number": 1
},
{
"number": 2
}
]
}
I'm looking for the best way to bine json files in a folder.
With HTML, CSS and JavaScript this is pretty easy since you don't need a separator or only a single ;
.
However, with JSON, we need something more to make it a valid JSON object.
One way would be to concatenate the files with ,
and wrap everything in an array. I'm wondering if there is an better/easier way to do this.
Gruntfile.js
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
json: {
src: ['src/**/*.json'],
dest: 'dist/bined.json',
options: {
...
}
}
}
});
src/file1.json
{
"number": 1
}
src/file2.json
{
"number": 2
}
dist/bined.json
This would be the desired oute:
{
"numbers": [
{
"number": 1
},
{
"number": 2
}
]
}
Share
Improve this question
edited Apr 11, 2013 at 2:13
christianvuerings
asked Apr 11, 2013 at 2:04
christianvueringschristianvuerings
23.2k4 gold badges24 silver badges20 bronze badges
3
- So basically you are trying to merge on same level keys? – Dan Kanze Commented Apr 11, 2013 at 2:13
- @DanKanze just trying to merge JSON files into another JSON file. Doesn't really matter what the contents of those files is. – christianvuerings Commented Apr 11, 2013 at 2:33
- @debuzze So that means you are trying to preserve key heirachy right? – Dan Kanze Commented Apr 11, 2013 at 2:36
3 Answers
Reset to default 4You should be able to use the banner and footer options.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
json: {
src: ['src/**/*.json'],
dest: 'dist/bined.json',
options: {
// Added to the top of the file
banner: '{"numbers": [',
// Will be added at the end of the file
footer: "]}",
separator: ','
}
}
}
});
You should use a dedicated and JSON oriented plugin to handle that. Not just an arbitrary string concat.
Take a look at https://github./rse/grunt-merge-json or https://github./shinnn/grunt-merge-data
You may use grunt-merge-json for that.
Usage Example:
Assuming we have the following types of source JSON files:
src/foo/foo-en.json:
{
"foo": {
"title": "The Foo",
"name": "A wonderful ponent"
}
}
src/bar/bar-en.json:
{
"bar": {
"title": "The Bar",
"name": "An even more wonderful ponent"
}
}
Assuming we want to generate the following destination JSON file:
{
"foo": {
"title": "The Foo",
"name": "A wonderful ponent"
},
"bar": {
"title": "The Bar",
"name": "An even more wonderful ponent"
}
}
Single file per target variant
grunt.initConfig({
"merge-json": {
"en": {
src: [ "src/**/*-en.json" ],
dest: "www/en.json"
},
"de": {
src: [ "src/**/*-de.json" ],
dest: "www/de.json"
}`enter code here`
}
});
Multiple files per target variant
grunt.initConfig({
"merge-json": {
"i18n": {
files: {
"www/en.json": [ "src/**/*-en.json" ],
"www/de.json": [ "src/**/*-de.json" ]
}
}
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745347452a4623622.html
评论列表(0条)