How to use the jq command under Linux
This article mainly introduces the relevant knowledge of how to use the jq command under Linux, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the jq command under Linux. Let's take a look.
Jq is a lightweight json processing command. Json data can be shredded, filtered, mapped, and transformed. Install. [root@test-dhcp ~] # yum install jq uses an example to create [root@test-dhcp ~] # jq-n {root@test-dhcp 1} {"a": 1} [root@test-dhcp ~] # jq-n'{a: "test"}'{"a": "test"} merge [root@test-dhcp ~] # jq-n'{a: "test"} + {test 2}'{"a": "test" "b": 2} [root@test-dhcp ~] # jq-n'{a: "test"} + {c: "testc"}'{"a": "test", "b": 2, "c": "testc"} Delete [root@test-dhcp ~] # cat test.json {"a": "test", "b": 2 "c": "testc"} [root@test-dhcp ~] # cat test.json | jq. {"a": "test", "b": 2, "c": "testc"} [root@test-dhcp ~] # cat test.json | jq 'del (.b)' {"a": "test", "c": "testc"} Update [root@test-dhcp ~] # cat test.json {"a": "test", "b": 2 "c": "testc"} [root@test-dhcp ~] # cat test.json | jq'.b = "testb"'{"a": "test", "b": "testb", "c": "testc"} [root@test-dhcp ~] # cat test.json | jq'. + {root@test-dhcp 4}'{"a": "test", "b": 2, "c": "testc", "d": 4} [root@test-dhcp ~] # cat test.json | jq'. + {dd:5 4}'| jq'.d = {dd:5}'{"a": "test", "b": 2, "c": "testc", "d": {"dd": 5}} query [root@test-dhcp ~] # cat test.json | jq. {"a": "test", "b": 2, "c": "testc" "d": {"dd": 5}} [root@test-dhcp ~] # cat test.json | jq'. + {jq'.d = {dd:5}'| jq .d.dd5 [root@test-dhcp ~] # echo'{"a": 1, "b": 2}'| jq'[.a, .b]'[1,2] View the data type [root@test-dhcp ~] # echo "{}" | jq-r typeobject [root@test-dhcp ~] # echo'[0, false, [], {}, null, "hello"]'| jq 'map (type)' ["number" "boolean", "array", "object", "null", "string"] query the values in the array [root@test-dhcp ~] # echo [1je 2jue 3] | jq. [1] 2 [root@test-dhcp ~] # echo [1JI 2jue 3] | jq. [2] 3 query array length [root@test-dhcp ~] # echo [1JI 2J J 9] | jq'. | length'4 [root@test-dhcp ~] # echo [1Q 2] 3] | jq'. | add up the length'3 array [root@test-dhcp ~] # echo [1pr 2pm 3] | jq'. + [4 echo 5 echo 6]'[1 map 2 map 3, 4 5 map 6] Advanced query [root@test-dhcp ~] # Personality [1 Magne 2 Jol 3] | > = 2))'[2 map 3] [root@test-dhcp ~] # echo [1 jq 'map (select (. = = 2))'[2] [root@test-dhcp ~] # echo [1 jq 2 map (select (. ! = 2)'[1,3] [root@test-dhcp ~] # cat test.json [{"id": "0", "model": "Intel (R) Xeon (R) CPUE5-2620v4@2.10GHz"}, {"id": "1" "model": "Intel (R) Xeon (R) CPUE5-2620v4@2.10GHz"}] [root@test-dhcp ~] # cat test.json | jq. [] .model "Intel (R) Xeon (R) CPUE5-2620v4@2.10GHz"Intel (R) Xeon (R) CPUE5-2620v4@2.10GHz" type conversion [root@test-dhcp ~] # echo'["a", "bjorie cjindjd", "e"]'| jq 'join (",")' "a paraphrase bPermie" E "[root@test-dhcp ~] # echo'[" a "," bdcec d "," e ", 1]'| jq 'join (", ")' jq: error (at: 1): string (", ") and number (1) cannot be added [root@test-dhcp ~] # cat test.jsonliuxin,30,malejiaweiqiang,29,femal [root@test-dhcp ~] # jq-R 'split (", ") | {" name ":. [0]," age ":. [1] "sex":. [2]}'. / test.json {"name": "liuxin", "age": "30", "sex": "male"} {"name": "jiaweiqiang", "age": "29", "sex": "femal"} [root@test-dhcp ~] # cat test.json {"name": "liuxin", "age": "30" "sex": "male"} {"name": "jiaweiqiang", "age": "29", "sex": "femal"} [root@test-dhcp ~] # cat test.json | jq. -c {"name": "liuxin", "age": "30", "sex": "male"} {"name": "jiaweiqiang", "age": "29", "sex": "femal"} this is the end of the article on "how to use jq commands under Linux". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use jq commands under Linux". If you want to learn more, you are welcome to follow the industry information channel.