How to convert a json response into yaml in bash

后端 未结 6 1815
不知归路
不知归路 2020-12-31 11:04

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

相关标签:
6条回答
  • 2020-12-31 11:45

    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
    
    0 讨论(0)
  • 2020-12-31 11:46
    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

    0 讨论(0)
  • 2020-12-31 11:50

    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
    
    0 讨论(0)
  • 2020-12-31 11:55

    I suggest using yq with -y option

    $ pip3 install yq # requires jq
    
    $ cat in.json | yq -y
    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
    
    0 讨论(0)
  • 2020-12-31 11:57

    With yq version 3.3.2:

    cat $DEFAULTS_FILE | yq r -P -

    yq a yaml wrapper for jq

    r read

    -P --prettyPrint

    - from STDIN

    0 讨论(0)
  • 2020-12-31 12:02

    Another oneliner:

    python -c 'import yaml, sys; print(yaml.dump(yaml.load(open(sys.argv[1])), default_flow_style=False))' 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
    
    0 讨论(0)
提交回复
热议问题