This examples can be executed directly from the current directory. Outcome of helm-data-json.sh is always JSON. Inputs could be YAML or JSON.
This merges YAMLs set1.yaml and set2.yaml together.
helm-data-json.sh -f ./eg_files/set1.yaml -f ./eg_files/set2.yaml
This merges JSONs _set1.json and _set2.json together.
helm-data-json.sh -f ./eg_files/set1.json -f ./eg_files/set2.json
This merges mix YAMLs and JSONs _set1.yaml and _set2.json together.
helm-data-json.sh -f ./eg_files/set1.yaml -f ./eg_files/set2.json
The output is same in all of those merges:
{"Apps":{"Set1":"Application1","Set2":"Application2"},"Last_set":"Set2"}The key Last_set is defined at both YAML/JSON files. So the value is same as at the last file which is defined with -f. If you run:
helm-data-json.sh -f ./eg_files/set2.yaml -f ./eg_files/set1.yaml
The output:
{"Apps":{"Set1":"Application1","Set2":"Application2"},"Last_set":"Set1"}There's several differnet ways to set the values for the single keys. You combine multiple setting under single --set statement, or define --set multiple times.
Single --set value is always in YAML format.
Adding new value:
helm-data-json.sh -f ./eg_files/set1.yaml --set Apps.Set3="Hello world"
The output:
{"Apps":{"Set1":"Application1","Set3":"Hello world"},"Last_set":"Set1"}Replacing the value:
helm-data-json.sh -f ./eg_files/set1.yaml --set Apps.Set1="Hello world"
The output:
{"Apps":{"Set1":"Hello world"},"Last_set":"Set1"}Arrays manipulations are a bit more complex. First we change the title of Teemu Vesala at file array.yaml:
helm-data-json.sh -f ./eg_files/array.yaml --set Users[0].Role="Public Clown"
Output:
{"Last_set":"Array Yaml","Users":[{"Name":"Teemu Vesala","Role":"Public Clown","Username":"tvesala"},{"Name":"Sakari Hoisko","Role":"Senior DevOps Consultant","Username":"shoisko"}]}oAdding item:
helm-data-json.sh -f ./eg_files/array.yaml \
--set Users[2].Name="Charlie Brown",Users[2].Username=cbrown,Users[2].Role="Comic Character"
Output:
{"Last_set":"Array Yaml","Users":[{"Name":"Teemu Vesala","Role":"Senior DevOps Consultant","Username":"tvesala"},{"Name":"Sakari Hoisko","Role":"Senior DevOps Consultant","Username":"shoisko"},{"Name":"Charlie Brown","Role":"Comic Character","Username":"cbrown"}]}The new list can be added:
helm-data-json.sh -f ./eg_files/array.yaml \
--set Passwords='{abc,def}'
Output:
{"Last_set":"Array Yaml","Passwords":["abc","def"],"Users":[{"Name":"Teemu Vesala","Role":"Senior DevOps Consultant","Username":"tvesala"},{"Name":"Sakari Hoisko","Role":"Senior DevOps Consultant","Username":"shoisko"}]}Creating the list of structure. Here we are using multiple --set commands to help the readability of the command.
helm-data-json.sh -f ./eg_files/array.yaml \
--set Passwords={},Passwords[0].User=tvesala,Passwords[0].Password=abc \
--set Passwords[1].User=shoisko,Passwords[1].Password=123dsa
Output:
{"Last_set":"Array Yaml","Passwords":[{"Password":"abc","User":"tvesala"},{"Password":"123dsa","User":"shoisko"}],"Users":[{"Name":"Teemu Vesala","Role":"Senior DevOps Consultant","Username":"tvesala"},{"Name":"Sakari Hoisko","Role":"Senior DevOps Consultant","Username":"shoisko"}]}This tool can construct the new YAML file dynamically with --set-parameters.
helm-data-json.sh \
--set Passwords={},Passwords[0].User=tvesala,Passwords[0].Password=abc \
--set Passwords[1].User=shoisko,Passwords[1].Password=123dsa
Output:
{"Passwords":[{"Password":"abc","User":"tvesala"},{"Password":"123dsa","User":"shoisko"}]}This example merges two files and adds new data to it, but also replaces the keys.
helm-data-json.sh -f ./eg_files/set1.yaml -f ./eg_files/set2.yaml \
--set Passwords={},Passwords[0].User=tvesala,Passwords[0].Password=abc \
--set Passwords[1].User=shoisko,Passwords[1].Password=123dsa \
--set Last_set="Dynamically created"
Output:
{"Apps":{"Set1":"Application1","Set2":"Application2"},"Last_set":"Dynamically created","Passwords":[{"Password":"abc","User":"tvesala"},{"Password":"123dsa","User":"shoisko"}]}The temlate is written to the standard output. Writing it to the file can be done two different ways:
helm-data-json.sh -f ./eg_files/set1.yaml -f ./eg_files/set2.yaml >output.yaml
This writes the output to output.yaml file and does not print anything to the screen. To write to the file and screen:
helm-data-json.sh -f ./eg_files/set1.yaml -f ./eg_files/set2.yaml | tee output.yaml
This can also merge multiple JSON files and convert them to JSON file.
helm-data-json.sh -f eg_files/test.json -f eg_files/set1.json -f eg_files/set2.json
Output:
{"Apps":{"Set1":"Application1","Set2":"Application2"},"Last_set":"Set2","Users":[{"Name":"Teemu Vesala","Username":"tvesala"},{"Name":"Sakari Hoisko","Username":"shoisko"}]}The JSON can be manipulaed same way as YAML. You can merge multiple JSON files, change their keys etc.