How to convert a json response into yaml in bash

convert json to yaml
python convert json to yaml
convert json to yaml nodejs
yq filter
bash parse yaml
yq replace value
spring convert json to yaml
yq write

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 YOUR_KEY@YOURHOST.local"]
    }
  ]
  }

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...

I'm not sure what rules you're using to get to your expected result. It seems like you're randomly applying different rules to how the values are being converted.

As I understand it, scalar values are just output as is (with potential encoding), objects are output as key/value pairs, and array objects are output with a - for every item. The indentation associates what's part of what.

So based on those rules if you're going to use jq:

def yamlify:
    (objects | to_entries[] | (.value | type) as $type |
        if $type == "array" then
            "\(.key):", (.value | yamlify)
        elif $type == "object" then
            "\(.key):", "    \(.value | yamlify)"
        else
            "\(.key):\t\(.value)"
        end
    )
    // (arrays | select(length > 0)[] | [yamlify] |
        "  - \(.[0])", "    \(.[1:][])"
    )
    // .
    ;

Then to use it, add it to your .jq file and use it:

$ jq -r yamlify input.json
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 YOUR_KEY@YOURHOST.local

Here's another variation that aligns the values

def yamlify2:
    (objects | to_entries | (map(.key | length) | max + 2) as $w |
        .[] | (.value | type) as $type |
        if $type == "array" then
            "\(.key):", (.value | yamlify2)
        elif $type == "object" then
            "\(.key):", "    \(.value | yamlify2)"
        else
            "\(.key):\(" " * (.key | $w - length))\(.value)"
        end
    )
    // (arrays | select(length > 0)[] | [yamlify2] |
        "  - \(.[0])", "    \(.[1:][])"
    )
    // .
    ;
$ jq -r yamlify2 input.json
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 YOUR_KEY@YOURHOST.local

Convert JSON to YAML, ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))' must have node and npm installed to install/use this Show Sample Output. How can I parse json response and extract the key from it in shell script? I don't want to install any library to do this since my JSON response is fixed and it will always be same as shown above so any simpler way is fine. Update:-Below is my final shell script -

I've used ruby to write my json content into yaml.

As for your example, it can be achieved like this:

cat $DEFAULTS_FILE | jq .users | ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))' > my.yml

How to convert a json response into yaml in bash, 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. 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 "

Another oneliner:

python -c 'import yaml, sys; print(yaml.dump(yaml.load(open(sys.argv[1]))))' input.json

(exploiting the fact that valid json is also valid yaml)

And yaml to json:

python -c 'import yaml, json, sys; print(json.dumps(yaml.load(open(sys.argv[1])), indent=2))' input.yaml

How to convert a json response into yaml in bash, 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. Stack Overflow Public questions and answers; To load JSON object into a bash associative array efficiently (without using loops in bash), one can use tool 'jq

function yaml_validate {
  python -c 'import sys, yaml, json; yaml.safe_load(sys.stdin.read())'
}

function yaml2json {
  python -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read())))'
}

function yaml2json_pretty {
  python -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read()), indent=2, sort_keys=False))'
}

function json_validate {
  python -c 'import sys, yaml, json; json.loads(sys.stdin.read())'
}

function json2yaml {
  python -c 'import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))'
}

More Bash tricks at http://github.com/frgomes/bash-scripts

yq: Command-line YAML/XML processor, yq takes YAML input, converts it to JSON, and pipes it to jq: cat input.yml | yq Use the --yaml-output / -y option to convert it back into YAML: cat input.yml | yq -y​  YAML vs JSON. when to prefer one over the other YAML. stands for YAML ain't markup language and is a superset of JSON .yml files begin with '---', marking the start of the document key value pairs are separated by colon lists begin with a hyphen JSON. a subset of the JavaScript object notation syntax

I'v been able to get the result using the following command:

jq -r .users defaultsfile.txt >> users.yaml

The output is in clear yaml format.

Transform JSON into YAML, Json to yaml converter. World's simplest yaml tool. Quickly convert JavaScript Object Notation to Yet Another Markup Language. Enter JSON in the input box on the  This YAML to JSON converter transforms YAML (Yet Another Markup Language) configs and files into JSON (JavaScript Object Notation) configs and files. It doesn't have any unncessary configuration options. The only thing you can configure is JSON's output. There are three options: indent JSON with spaces, indent JSON with tabs, or compress JSON

How to parse a json file from Linux command line using jq , How to parse a json file from Linux command line using jq The JSON (​JavaScript Object Notation) format is widely used to represent data in that case binaries would be installed into $HOME/.local/bin and libraries into the  The output will display below the Convert button. See also JSON to YAML Option 1 - Choose a YAML file Encoding Option 2 - Enter an URL Option 3 - paste into Text Box below

json2yaml, A commandline utility to convert JSON to YAML / YML. The purpose of this utility is to pretty-print JSON in the human-readable YAML object  YAML vs JSON. when to prefer one over the other JSON. a subset of the JavaScript object notation syntax data stored in name/value pairs records separated by commas field names & strings are wrapped by double quotes YAML. stands for YAML ain't markup language and is a superset of JSON - Convert YAML to JSON

Bash yaml to json, Use this online tool to convert YAML into JSON. Run a Bash The output is shell code that defines shell variables which contain the parsed values. The script  yq. a lightweight and portable command-line YAML processor. The aim of the project is to be the jq or sed of yaml files.. New version! V3 is officially out - if you've been using v2 and want/need to upgrade, checkout the upgrade guide.

Comments
  • You can use python/perl which have in-built YAML and JSON modules - commandlinefu.com/commands/view/12218/convert-yaml-to-json
  • That's exact the opposite direction
  • Bash itself is not a good platform for this. Find an existing tool, or write one in e.g. Python. Depending on your preferred language, it should not be hard to find an existing question on Stack Overflow with suggestions; here's a search for Python
  • Please follow the minimal reproducible example guidelines. In particular, a sample my.yml (i.e. input) would be helpful.
  • yq r input.json should do the job.
  • that only writes the json to a file. The file has a .yaml extension in the name, but the content is not yaml !