shell - jq processing json

Useful Link:

        offical manual link:jq Manual (development version) 

        Chinese version: jq Chinese manual (v1.5) | jq Chinese manual

        Common operations: jq common operations - Huang Huang's blog      

        Processing example: Shell: an extremely powerful shell json parsing tool jq, Linux command line parsing json, jq parsing json instance

                        JSON formatted output and parsing tool - jq - Get rid of the glitz - 博客园

        code:GitHub - stedolan/jq: Command-line JSON processor  

        other than jq, use sh script to process json directly: https://github.com/dominictarr/JSON.sh

1. Ubuntu installation: sudo apt-get install jq

2. View jq help: man jq

3. Transmission parameters: --arg

arg="configuration1"
jq --arg arg "$arg" '.configurations[] | select(.name == $arg)' example.json

4. Print the key and value of json in the specified format: keys[] as $k (-r remove the double quotes, that is, from "a" -> a)

jq -r '.[] | keys[] as $k | "--\($k)=\(.[$k])"' example.json

5. Merge multiple json files:

jq -r -s '.[0] * .[1]' file1 file2
echo '{"A": {"a": 1}}' '{"A": {"b": 2}}' '{"B": 3}' |\
  jq --slurp 'reduce .[] as $item ({}; . * $item)'

6. Traverse the json array:

jq -c '.[test_group_1][]' example.json | while read test; do
    echo "test $test"
done

7. To change the value of a field:

jq '.test_group_1[] | select(.name == test_1-1).checksum = 1' example.json

8. Change the key of a field: with_entries( is equal to "from_entries, map, to_entries")

jq '. |= with_entries(.key |= sub("^test_group_1$"; "changed"))' example.json

example.json

{
    "configurations": [
        {
            "name": "configuration1",
            "parameters": {
                "itype": "xxx"
            }
        }
    ],
    "test_group_1": [
        {
            "name": "test_1-1",
            "config": "configuration1",
            "file": "testfile3",
            "checksum": "8a9d260e03e95e1b10a474bf82ed8ac7"
        },
        {
            "name": "test_1-2",
            "ignore": true,
            "config": "configuration1",
            "file": "testfile3",
            "parameters": {
                "duration": "70000"
            },
            "checksum": "d4622236d60df0098cc85861bc089e3f"
        }
    ]
}

Guess you like

Origin blog.csdn.net/iamanda/article/details/122234695