admin管理员组文章数量:1421250
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" ]
}
}
}
});
本文标签: javascriptCombine JSON files with gruntcontribconcatStack Overflow
版权声明:本文标题:javascript - Combine JSON files with grunt-contrib-concat - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745347452a2654557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论