Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a json response into yaml in bash

Tags:

json

bash

yaml

jq

I read data from a json file with jq. I wanna append the results into a yaml file, but I dont get it working. I am quite new to shell programming. My goal is to append that "users" to an existing "users"-Array in a yaml file.

This is my json file:

#$DEFAULTS_FILE  {"users":   [     {"name":"pi",       "gecos": "Hypriot Pirate",       "sudo":"ALL=(ALL) NOPASSWD:ALL",       "shell": "/bin/bash",       "groups":"users,docker,video",       "plain_text_passwd":"pi",       "lock_passwd":"false",       "ssh_pwauth":"true",       "chpasswd": {"expire": false}     },     {"name":"admin",       "gecos": "Hypriot Pirate",       "sudo":"ALL=(ALL) NOPASSWD:ALL",       "shell": "/bin/bash",       "primary-group": "users",       "groups":"users,docker,adm,dialout,audio,plugdev,netdev,video",       "ssh-import-id":"None",       "plain_text_passwd":"pi",       "lock_passwd":"true",       "ssh_pwauth":"true",       "chpasswd": "{expire: false}",       "ssh-authorized-keys": ["ssh-rsa abcdefg1234567890 [email protected]"]     }   ]   } 

I filter it with that:

cat $DEFAULTS_FILE | jq .users

I have no clue how to convert that json into a yaml.

My expected result should be:

users:   - name:                pi     gecos:               "Hypriot Pirate"     sudo:                ALL=(ALL) NOPASSWD:ALL     shell:               /bin/bash     groups:              users,docker,video     plain_text_passwd:   pi     lock_passwd:         false     ssh_pwauth:          true     chpasswd: { expire:  false }   - name:                admin     primary-group:       users     shell:               /bin/bash     sudo:                ALL=(ALL) NOPASSWD:ALL     groups:              users,docker,adm,dialout,audio,plugdev,netdev,video     ssh-import-id:       None 

I tried to use a second tool called yq which is similar to jq and can write yaml files. But I have no positive progress.

EDIT

I know that I can add content to the yaml with that:

yq w -i "my.yml" "users[+]" "some content"

But I dont know how to merge my json into that.

Any help or hint would be nice, thank you in advance...

like image 555
Jan Avatar asked Nov 15 '18 09:11

Jan


People also ask

Can I convert JSON to YAML?

Users can also Convert JSON File to YAML by uploading the file. When you are done with JSON to YAML converting. You can download as a file or create a link and share. JSON to YAML Transformer works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.

Can you use jq for YAML?

The -Y option is incompatible with jq filters that do not expect the extra information injected into the document to preserve the YAML formatting. For example, a jq filter that counts entries in the Instances array will come up with 4 entries instead of 2.

Is JSON valid in YAML?

YAML 1.2 is (with one minor caveat regarding duplicate keys) a superset of JSON, so any valid JSON file is also a valid YAML file. However, the YAML 1.1 specification (which has the most library support) doesn't mention JSON.

Can bash handle JSON?

Unfortunately, shells such as Bash can't interpret and work with JSON directly. This means that working with JSON via the command line can be cumbersome, involving text manipulation using a combination of tools such as sed and grep.


1 Answers

yq a yaml wrapper for jq

With yq version 4.8.0:

cat $DEFAULTS_FILE | yq e -P -

  • e or eval handles file separately. ea or eval-all will merge files first.
  • -P or --prettyPrint YAML output
  • - from STDIN

Note: you can go the other way (yaml to json) too yq e -j file.yaml

With yq version 3.3.2:

cat $DEFAULTS_FILE | yq r -P -

  • r read
  • -P --prettyPrint
  • - from STDIN
like image 144
ianmunoz Avatar answered Oct 13 '22 15:10

ianmunoz