Skip to content

Commit e1344fc

Browse files
committed
Doc update
1 parent 0f4d91c commit e1344fc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,37 @@ Show all book titles by authors starting with 'Doug'.
276276
'$..book[?(@.author="Doug")].["author","title",rating]'
277277
```
278278

279+
### Modification
280+
281+
A common task is to modify JSON data on-the-fly. Unix style 'one-liners' can be
282+
created to easily and logically modify JSON data.
283+
284+
The key to data modification (that is: add, modify or deletion of data) is to
285+
modify the *JSON.sh* formatted data, then use the passthrough, '-p', option to
286+
output again as JSON. Sequence is:
287+
288+
1. Read JSON data and output as *JSON.sh* data.
289+
2. Modify *JSON.sh* data using standard Unix tools.
290+
3. Pipe modified *JSON.sh* data to JSONPath.sh with passthrough option producing
291+
JSON data again.
292+
293+
For example: The following 'one-liner' will read a kubernetes deployment
294+
configuration (using 'kubectl get ...'), output it in *JSON.sh* format (using
295+
'JSONPath.sh'), change the number of replicas from the existing value to 5
296+
(using *sed*), output again in JSON (using 'JSONPath.sh -p'), then replace the
297+
original deployment with the newly changed one (using 'kubectl replace ...'.
298+
299+
```
300+
kubectl get deployment sample-deployment -o json | \
301+
JSONPath.sh | \
302+
sed 's/\["spec","replicas"\].*/["spec","replicas"]\t'"$2"'/' | \
303+
JSONPath.sh -p | \
304+
kubectl replace deployment sample-deployment -f -
305+
```
306+
307+
This allows you to reuse your Unix skills rather than learn new terse syntax
308+
or Domain Specific Language.
309+
279310
### Re-injection
280311

281312
This tool, JSONPath.sh, is really handy for handing json formatted

0 commit comments

Comments
 (0)