json1-5 Exercise 2: Entries, from_entries Shapes and Paths ========================================================= Run in Git Bash with jq 1.8.2 (the Chapter 4 install in C:/lwdj/json/c4/bin): bash ex2.sh --- doc.json --- {"a":{"b":[10,{"c":null}],"d":"x"},"e":true} --- ex2.sh --- # Exercise 2: entries, from_entries key names, and paths export PATH="/c/lwdj/json/c4/bin:$PATH" echo '--- 1. owner object: drop nulls, upper-case keys, default values' jq -c '.owner | with_entries(select(.value != null))' shop.json jq -c '.owner | with_entries(.key |= ascii_upcase)' shop.json jq -c '.owner | with_entries(.value |= (. // "n/a"))' shop.json echo '--- 2. which entry shapes does from_entries accept? (jq 1.8.2)' for e in '{"key":"a","value":1}' '{"name":"a","value":1}' '{"Name":"a","Value":1}' '{"Key":"a","Value":2}' '{"k":"a","v":1}' '{"key":"a","v":1}' '{"K":"a","V":1}' '{"key":5,"value":1}' '{"key":true,"value":1}' '{"key":null,"value":1}' '{"key":"a"}'; do printf '%-28s -> ' "$e"; jq -nc "[$e] | from_entries" 2>&1 | tr -d '\r' done echo '--- 3. building an object from an array, three ways' jq -c '.products | map({(.sku): .stock}) | add' shop.json jq -c '.products | map({key: .sku, value: .stock}) | from_entries' shop.json jq -c '.products | INDEX(.sku) | map_values(.stock)' shop.json echo '--- 4. paths in doc.json' cat doc.json jq -c '[paths]' doc.json jq -c '[paths(scalars)]' doc.json jq -c 'getpath(["a","b",1,"c"])' doc.json jq -c 'getpath(["a","nope","x"])' doc.json echo ' ^ both are null: getpath cannot tell "null" from "missing". Use has/paths to tell them apart:' jq -c '[paths] | (map(. == ["a","b",1,"c"]) | any), (map(. == ["a","nope","x"]) | any)' doc.json jq -c 'setpath(["a","b",0]; 99) | .a.b' doc.json jq -c 'setpath(["new","deep"]; 1) | .new' doc.json jq -c 'del(.a.b[1], .e)' doc.json jq -c 'delpaths([["a","d"],["e"]])' doc.json echo '--- 5. walk, with a count before and after as the check' jq -c '[.. | numbers] | length' doc.json jq -c 'walk(if type == "number" then . + 1 else . end)' doc.json jq -c 'walk(if type == "number" then . + 1 else . end) | [.. | numbers]' doc.json jq -c 'walk(if type == "object" then del(.c) else . end)' doc.json echo '--- 6. pick (jq 1.7+)' jq -c 'pick(.a.d, .e)' doc.json jq -c 'pick(.a.b[1].c)' doc.json jq -c 'pick(.zzz)' doc.json echo '--- 7. leaf_paths no longer exists' jq -c '[leaf_paths]' doc.json 2>&1 | head -1 Output: --- 1. owner object: drop nulls, upper-case keys, default values {"first":"Ada","last":"Lovelace"} {"FIRST":"Ada","LAST":"Lovelace","PHONE":null} {"first":"Ada","last":"Lovelace","phone":"n/a"} --- 2. which entry shapes does from_entries accept? (jq 1.8.2) {"key":"a","value":1} -> {"a":1} {"name":"a","value":1} -> {"a":1} {"Name":"a","Value":1} -> {"a":1} {"Key":"a","Value":2} -> {"a":2} {"k":"a","v":1} -> jq: error (at ): Cannot use null (null) as object key {"key":"a","v":1} -> {"a":null} {"K":"a","V":1} -> jq: error (at ): Cannot use null (null) as object key {"key":5,"value":1} -> jq: error (at ): Cannot use number (5) as object key {"key":true,"value":1} -> jq: error (at ): Cannot use boolean (true) as object key {"key":null,"value":1} -> jq: error (at ): Cannot use null (null) as object key {"key":"a"} -> {"a":null} --- 3. building an object from an array, three ways {"A1":40,"B2":0,"C3":120,"D4":15} {"A1":40,"B2":0,"C3":120,"D4":15} {"A1":40,"B2":0,"C3":120,"D4":15} --- 4. paths in doc.json {"a":{"b":[10,{"c":null}],"d":"x"},"e":true} [["a"],["a","b"],["a","b",0],["a","b",1],["a","b",1,"c"],["a","d"],["e"]] [["a","b",0],["a","d"],["e"]] null null ^ both are null: getpath cannot tell "null" from "missing". Use has/paths to tell them apart: true false [99,{"c":null}] {"deep":1} {"a":{"b":[10],"d":"x"}} {"a":{"b":[10,{"c":null}]}} --- 5. walk, with a count before and after as the check 1 {"a":{"b":[11,{"c":null}],"d":"x"},"e":true} [11] {"a":{"b":[10,{}],"d":"x"},"e":true} --- 6. pick (jq 1.7+) {"a":{"d":"x"},"e":true} {"a":{"b":[null,{"c":null}]}} {"zzz":null} --- 7. leaf_paths no longer exists jq: error: leaf_paths/0 is not defined at , line 1, column 2: WHY THIS WORKS AS AN ANSWER --------------------------- Section 2 records what from_entries really does on 1.8.2 instead of trusting memory: key/value, name/value, Name/Value and Key/Value work; k/v and K/V fail; a wrong VALUE name (key + v) does not fail, it silently gives null; numeric, boolean and null keys fail. Section 4 shows getpath returning null both for a real null and for a path that does not exist, and an existence check that returns true for a real path and false for a made-up one. That check is the control: my first version used index() on the list of paths and said false for a path that does exist (index on an array of arrays looks for a sub-array). The walk lines show a count of numbers before (1) and after the +1 (still one, now 11), so the change is verified by a number, not by eye. pick pads a missing path with null ({"zzz":null}) and an array index with nulls. leaf_paths gives "is not defined": it was removed in jq 1.7 (this is from the release notes and from running it).