4

Input:

{"success": true, "results": {"a": …, "b": …, "c": …}}

Desired output, given I want to keep b:

{"success": true, "results": {"b": …}}

Things I tried:

$ jq 'del(select(.results.b | not))'
{"success": true, "results": {"a": …, "b": …, "c": …}}
# removes nothing from "results"

$ jq 'with_entries(select(.key == "success" or .key == "results.b"))'
{"success": true}
# nested comparison not understood; returns only "success"

Thanks!

peak
  • 105,803
  • 17
  • 152
  • 177
zopieux
  • 2,854
  • 2
  • 25
  • 32

2 Answers2

5

Here is one solution:

.results |= {b}

Sample Run

$ jq -M '.results |= {b}' <<< '{"success":true, "results":{"a": "…", "b": "…", "c": "…"}}'
{
  "success": true,
  "results": {
    "b": "…"
  }
}

Try it online at jqplay.org

jq170727
  • 13,159
  • 3
  • 46
  • 56
0

Another way using and :

Code :

$ node<<EOF
var obj = $(</tmp/file.json);
delete obj.results.a;
delete obj.results.c;
console.log(JSON.stringify(obj));
EOF

OUTPUT :

{"success":true,"results":{"b":"bbb"}}
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223