grunt-update-json
Merge parts from one or more JSON files together. I use grunt-update-json
to keep my bower.json
and component.json
in sync with package.json
.
Upgrading from 1.x
The semantics of Object Groupings have been reversed:
- was
{from: "to"}
- now``{to: "from"}`
Getting Started
npm install grunt-update-json --save-dev
I highly favor using the fabulous load-grunt-tasks
over the tiring and cumbersome grunt.loadNpmTasks
. Your grunt tasks are all in your package.json
, so let's embrace DRY:
npm install load-grunt-tasks --save-dev
// Gruntfile.jsmodule { grunt; // load all grunt tasks. Done!
update_json
task
Using the In your awesome project's Gruntfile, add a section named update_json
:
// Gruntfile.jsgrunt;
API
options
Like most Grunt tasks, options can be specified at the update_json
level
and/or at the update_json:<target>
level. Target-level options
override task-level options
.
options.indent
By default, output will not be pretty-printed. Specify a value here to have indentation applied:
update_json: options: indent: "\t"
or for spaces:
update_json: options: indent: " "
Source Data
src
An input JSON file. May be a list, which will be [
_.merge
d][_merge] together. [_merge]: http://lodash.com/docs#merge
Destination Data
dest
An output JSON file.
Field Groupings
fields
an ordered collection of field specifications, which can optionally contain additional lists of fields.
Object Grouping
{fields: {field: null, another: "yetanother"}}
A list of field specs, pointing at any other kind of field specification.
Array Grouping
{fields: ["field", "another", "still another > yet another"]}
Create field copies, or field renames, of each of the listed fields.
String Grouping
{fields: "field, another, still another > yetanother"}
Create field copies, or field renames, of each of the listed fields.
The most concise way to copy/rename a number of fields of simple JSON documents
Limitations
- Can't handle fields with
,
or>
in their names. - Can't handle most complex field paths.
Field Specifications
The canonical Object Grouping format is used here: some specifications are not compatible with some Groupings.
Field Copy
{field: null}
Create or replace
field
indest
from the value offield
insrc
.
Field Rename
{renamed: "original"}
or"original > renamed"
String Grouping onlyCreate or replace
renamed
indest
with the value oforiginal
fromsrc
.
Field Pointer
{field: "/some/deep/field"}
Create or replace
field
indest
fromsome/deep/field
insrc
.
A field spec destination which starts with /
will be interepreted as a
json-pointer.
To select a field that begins with a literal /
, escape with a single \
(written \\
):
{field: "\\/a"}
Field Path
{field: "$.some.path[(@.with='filters')]"}
Create or replace
field
indest
with the value of nodes found with a JSONPath query
A field spec destination which starts with $.
will be interpreted as a
JSONPath selector.
To select a field that begins with a literal $.
, escape with a single \
(written \\
):
{field: "\\$.a"}
Field Collapse
{field: ["first", "second"]}
Create or replace an array named
field
indest
with the values offirst
andsecond
fromsrc
.
Field Construct
{field: {first: "first", second: "second"}}
Create or merge an object
field
indest
with labeledfirst
andsecond
with their respective values fromsrc
Field Function
{field: function(src){ return src.field; }}
Create a field named
field
that is the output of running a function againstsrc
.
If all else fails, you can supply a function which will called with a copy of the combined source object.
update_json: composer: src: "package.json" dest: "composer.json" fields: { // pull username/repo off a github url return srcrepositoryurl1; }
Ideas for improvement
- see enhancements
License
MIT