This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
269 lines (194 loc) · 7.52 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
json_merger(1) General Commands Manual json_merger(1)
NAME
json_merger - Merge JSON files based on indicators
SYNOPSIS
json_merger [OPTION]... [file]...
DESCRIPTION
json_merger is a way to merge JSON files based on indicators such as
@extends, @match, @override and @delete. Only works for a ROOT NODE
OPTIONS
-p, --pretty
Prettify, indent the output JSON in a human way.
-s, --suffix <.suffix>
Output suffix. Suffix added to input files to construct output
files. If left blank then stdout is used.
-v, --variable <key=value>
Set key=value variables. See "PATH TEMPLATING".
-h, --help
Show help and exit.
-V, --version
Print version information.
INDICATORS
@extends
An array or string of path names to files which should be extended.
@override
An array, string or true indicating that the given property will be
overridden. MATCH SYNTAX is supported. See "@delete"
@delete
An array, string or true indicating that the given property will be
deleted. When used with true the whole node will be overridden.
When used with an array only the listed properties will be overrid-
den. MATCH SYNTAX is supported.
@delete will always delete the listed nodes, while ignoring all
regular properties, while @override will empty the listed nodes,
and merge ontop of that, this will also leave an empty object.
@append, @prepend, @insert
When working with arrays the default behaviour will be to merge on
indexes, i.e. first item in the arrays will be merged together.
Using @append one can force the node to be appended at the end of
the destination array.
Using @prepend one can force the node to be prepended at the begin-
ning of the destination array. Note that @prepend will insert at
index 0, 1, 2 accordantly to make the JSON represent the sequence
of which the nodes are inserted.
Using @insert one can specify the index at which the node should be
inserted.
@match
Match can be used to match a given item in an array. See "MATCH
SYNTAX" for more information.
@move
This indicator is the same as @insert, but is used together with
@match.
@value
Used together with the @insert family of indicators, to insert a
primitive value at a specific location. See "EXAMPLE" for an exam-
ple.
@comment
These will be removed in the final output file, and is intended to
be used for internal comments.
@id Global ID's that can be matched with @match.
PATH TEMPLATING
Paths specified in @extends can be templated using the following syn-
tax: $variable and ${variable}.
See the following example:
$ cat a.json
{ "@extends": ["${my_var}.json"], "a": 1 }
$ cat b.json
{ "b": 1 }
$ json_merger -v my_var=b a.json
{ "b": 1, "a": 1 }
MATCH SYNTAX
Property
Match a property with a primitive value, the syntax is:
[property]
[property=value]
[property='value']
[@SPECIAL=value]
If single quotes is used a string will be matches. Note that a
string will still be checked even when they are omitted.
Check for a property beginning, containing and ending with value
[property^=value]
[property*=value]
[property$=value]
The list of @SPECIAL are:
@value use to match primitives in arrays.
@id use to match specified ID's. See "INDICATORS".
ID Match an ID specified with @id. The syntax is:
#id
See "INDICATORS".
Directory
Match a property name and ascend into that matches node, the syntax
is:
name/
EXAMPLE
@extends
To extend the files c.json and then b.json one could write:
{
"@extends": ["b.json", "c.json"]
}
This will take b.json as the base file. Then apply c.json on top.
And finally apply the initial file on top of that output.
If b.json or c.json contains @extends indicators these will be
resolved beforehand.
@override
Having b.json as:
{
"prop1": { "b": 1 }
}
One can override prop1 with:
{
"@extends": ["b.json"],
"prop1": { "@override": true, "a": 1 }
}
The same can be archived using:
{
"@extends": ["b.json"],
"@override": ["prop1"],
"prop1": { "a": 1 }
}
@insert
Having b.json as:
{
"arr": ["A", "B", "C"]
}
One can insert an object between A and B with:
{
"@extends": ["b.json"],
"arr": [ { "@insert": 1, "a": 1 } ]
}
The same applies for @append and @prepend.
If one wants to insert a primitive value, or an array, one can use
@value. Taking the example from above:
{
"@extends": ["b.json"],
"arr": [ { "@insert": 1, "@value": "A2" }
}
Match Property
Having b.json as:
{
"arr": [
{ "a": 1 },
{ "a": 2 },
{ "a": 3 }
}
One can match the second element { "a": 2 } with:
{
"@extends": ["b.json"],
"arr": [ { "@match": "[a=2]" } ]
}
Match Directory
Having b.json as:
{
"a": {
"b": {
"c": 1
}
}
}
One can match the the path object containing c = 1 with:
{
"@extends": ["b.json"],
"@match": "a/b/[c=1]"
}
Match @value
Having b.json as:
{
"arr": [ "A", "B", "C" ]
}
One can match and delete the B with:
{
"@extends": ["b.json"],
"arr": [ { "@delete": true, "@match": "[@value=B]" } ]
}
Match @id
Having b.json as:
{
"a": { "@id": "a" },
"b": { "@id": "b" }
}
One can match and delete the a with:
{
"@extends": ["b.json"],
"@match": "#a",
"@delete": true
}
ROOT NODE
A root node is the outer most node in a JSON file. These can contain
@extends, but cannot be deleted nor overridden.
EXIT STATUS
The following exit values shall be returned:
0 Successful completion.
>0 An error occurred.
AUTHOR
Andreas Louv <andreas@louv.dk> is the author of json_merger.
json_merger(1)