Table of Contents:
.fromFile('file.json')
.fromObject(object)
- config
@extends
- Indicators
.merge(objA, objB)
- Command line interface
json_merger
- Expiremental usage
- Root indicators
- License
Apply indicators such as @insert
, @match
and @override
to tell the processor how to merge the files.
.fromFile('file.json')
var json_merger = require('json_merger');
var result = json_merger.fromFile('fileA.json');
fileA.json:
fileB.json:
Result:
.fromObject(object)
var json_merger = ; var objA = "@extends": "fileB.json" "prop1": "@override": true "prop_a": "this will override fileB.json's property prop1" "prop2": "prop_a": "some value" ; var result = json_merger;
fileB.json:
Result:
config
asText: false // true, false, 'pretty' javascript: false // true, false scope: '' // directory to look for initial file variables: // contains a key->value object with variables to @extends
config.asText
default: false
(in command line interface it will default to true)
Values are true
, false
, pretty
where pretty will indent the JSON with \t
for each block.
config.javascript
Expiremental
default: false
Preserve JavaScript functions, regexp, etc see Expiremental usage
config.scope
default: process.cwd()
The initial directory which the inputFile is relative too, will be overridden for each @extends
file and is set to dirname of current inputFile.
config.variables
default: {}
Variables is used in @extends
like the following:
json_merger;
fileA.json
Indicators:
@extends
An array / string indicating which files a given object extends, this is a root indicator
@override
An array or true
indicating that the given property will be overridden. When used with true
the whole property will be overridden. When used with an array the listed properties will be overridden:
Usage of true
b.json
Result
Usage of array
b.json
Result
@append
, @prepend
, @insert
When working with array the default behaviour will be to merge on indexes, eg first item in the arrays will be merged together etc.
@append: true
is alias for@insert: -1
@prepend: true
is alias for@insert: 0
Using @insert
:
b.json
Result
@match
Match can be used to match a given item in an array; Supported syntax:
[prop1=val1][prop2='val2']
You can ether use prop1, prop2 for matching regular properties @value
which will match the value of primitives in the array:
Quoting is optional but required if you want strict comparison, eg [prop='2']
will match {"prop": "2"}
and not {"prop": 2}
Usage
b.json
Result
Advanced usage
b.json
Result
Advanced Usage 2 (@value
)
b.json
Result
@move
This indicator is the same as @insert
but is used together with @match
.
@value
Used when merging arrays containing primitives or other arrays
fileB.json
Output
@comment
These will be removed in the merging process and is intented to be used for internal comments about overrides etc.
@id
This can be matches in @match
using the following syntax:
b.json
.merge(objA, objB)
You can use json_merger without having to use JSON stored in files, you can use it directly with JavaScript objects:
var json_merger = ; var a = a: "@override": true "my_value": 1234 ;var b = a: "my_b_value": 1234 var result = json_merger; console; // 1234console; // undefined
json_merger
Command line interface You can use json_merger as a command line tool:
Usage: json_merger <file> [OPTIONS]
Mandatory arguments to long options are mandatory for short options too.
-p, --pretty Prettify the output json
-j, --javascript Perserve JavaScript functions, regex, etc...
-v, --variables Send key=value list of variables
Usage -v key1=value1 -v key2=value2
-h, --help Show this help
-v, --version Show the version
Usage:
json_merger input.json > out.jsonjson_merger input.json --pretty > out.jsonjson_merger input.json --javascript > out.jsonjson_merger input.json -p -j -v "root=/var/www/" > out.json
Make sure that add json_merger to the $PATH
variable.
npm install -g json_merger
Experimental Usage:
Working with JavaScript code in JSON Use at own risk :-)
var json_merger = ; var output = json_merger;
file.jsonx
"@extends": "fileB.jsonx" { return } "obj": "a": { /* code here */ }1 2
fileB.jsonx
"@extends": "fileB.jsonx" "obj": "b": { /* another code here */ }1 2
Result:
{ return } "obj": "a": { /* code here */ }1 2 "b": { /* another code here */ }1 2
Root indicators
Root indicators is properties that are only supported on the outer most level of the JSON tree.
Current supported root indicators:
License:
The MIT License (MIT)
Copyright (c) 2015 Andreas Louv
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+++++ +++++ +[>+++++ +++++<-]>----.+++++++++.----.-.---------------.++++++++++++++.--------.+++++++++++++.-----------.--.+++++++++++++.