select all keys with given value - json

With jq, how do I select all objects, which may be nested, with a desired value?
For example, given the following:
{
"a": "b",
"c": {
"d": {
"e": "f",
"z": "b"
}
}
}
How do I filter down to objects whose value is "b"?
{
"a": "b",
"c": {
"d": {
"z": "b"
}
}
}
Conversely, how do I select objects whose value is not "b"?
{
"c": {
"d": {
"e": "f"
}
}
}
I've attempted at the problem by using the select and walk functions, but could not get exactly what I wanted.

For the first question:
reduce paths(. == "b") as $p ({}; setpath($p; "b"))
For the second:
reduce paths(. == "b") as $p (.; delpaths([$p]))
or even more succinctly:
delpaths( [ paths(. == "b") ] )
Furthermore
The first question can also be answered without reduce, but to get it right generically requires more verbosity, e.g.:
delpaths( [ paths( (. !="b") and (type|IN("object","array") | not) ) ] )

Related

jq: how to replace keys with values ​from other keys whose ​are in strings of some array

Consider following array:
{
"A": 100,
"B": 200,
"C": "ccc",
"arr": [
{
"result": ".R1.R3",
"fmt": "%s::%s::baz",
"vals": [".A", ".B"]
},
{
"result": ".R2.R4",
"fmt": "%s/%s",
"vals": [".A", ".C"]
}
]
}
I need to replace keys according some format with values ​​from other keys whose ​​are in strings of some array.
Desired output:
{
"A": 100,
"B": 200,
"C": "ccc",
"R1": {"R3": "100::200::baz"},
"R2": {"R4": "100/ccc"}
}
You didn't specify what language you use in the .vals array items to reference into the document. If you were trying to execute (arbitrary) jq code from there, know that jq cannot do that with code provided as a string value. jq also doesn't provide printf-style substitutions using %s (and others). Therefore, you either need to re-implement a whole bunch of (third-party) functionality, or revert to a simpler scheme describing your references and substitutions.
For the sake of simplicity, this solution just removes the first character (the dot) from the .vals array items and treats the result as top-level field name, and then simply replaces each occurrence of a literal %s with the next value. This should give you an overview of the general technique.
. as $top | reduce .arr[] as $a (del(.arr); .[$a.result] = (
reduce $a.vals[][1:] as $val ($a.fmt; sub("%s"; $top[$val] | #text))
))
{
"A": 100,
"B": 200,
"C": "ccc",
".R1": "100::200::baz",
".R2": "100/ccc"
}
Demo
One quite simple way of improving the reference language is to instead use path expressions jq provides functions for. They are represented as arrays with field names as strings items and array indices as number items. .A would become ["A"], .A[3].B would become ["A",3,"B"], and so on. Thus, assume your input looked like this:
{
"A": 100,
"B": 200,
"C": "ccc",
"arr": [
{
"result": ".R1",
"fmt": "%s::%s::baz",
"vals": [["A"], ["B"]]
},
{
"result": ".R2",
"fmt": "%s/%s",
"vals": [["A"], ["C"]]
}
]
}
Then you could use getpath to evaluate the given path expressions as above:
. as $top | reduce .arr[] as $a (del(.arr); .[$a.result] = (
reduce $a.vals[] as $path ($a.fmt; sub("%s"; $top | getpath($path) | #text))
))
{
"A": 100,
"B": 200,
"C": "ccc",
".R1": "100::200::baz",
".R2": "100/ccc"
}
Demo
Edit: As the question has been modified with the .result value now also being subject to reference interpretation, measures taken for .vals therefore apply to it as well. This implies changing the suggested source document format to use path expressions as in "result": ["R1", "R3"], and changing the assignment in the suggested code from .[$a.result] = ... to setpath($a.result; ...):
{
"A": 100,
"B": 200,
"C": "ccc",
"arr": [
{
"result": ["R1", "R3"],
"fmt": "%s::%s::baz",
"vals": [["A"], ["B"]]
},
{
"result": ["R2", "R4"],
"fmt": "%s/%s",
"vals": [["A"], ["C"]]
}
]
}
. as $top | reduce .arr[] as $a (del(.arr); setpath($a.result;
reduce $a.vals[] as $path ($a.fmt; sub("%s"; $top | getpath($path) | #text))
))
{
"A": 100,
"B": 200,
"C": "ccc",
"R1": {
"R3": "100::200::baz"
},
"R2": {
"R4": "100/ccc"
}
}
Demo

How to insert a field at the top of an object?

This is my code
element='{"x":"zero"}'
example='[
{
"a":"one",
"b":"two",
"c":"three"
},
{
"d":"four",
"e":"five",
"f":"six"
}]'
jq --argjson el "$element" '.[] += $el' <<< $example
It currently outputs
[
{
"a":"one",
"b":"two",
"c":"three",
"x":"zero"
},
{
"d":"four",
"e":"five",
"f":"six",
"x":"zero"
}
]
But i would like to have the "x":"zero" be the first element of all array elements, not the last.
Inverting the variable with the .[] iterator would make no sense. Any idea how this can be done?
In order for the new field to appear at the top, the object that holds it must appear first in the addition statement. For example:
$ jq --argjson el "$element" 'map($el + .)' <<< $example
[
{
"x": "zero",
"a": "one",
"b": "two",
"c": "three"
},
{
"x": "zero",
"d": "four",
"e": "five",
"f": "six"
}
]

Using jq to merge two arrays into one array of objects

I am very beginner into jq and I want to reshape my JSON file.
I have ve got JSON structured like this:
{
"a": [1, 2, 3, 4 ...],
"b": [
{
"x": 1000,
"value": 1
},
{
"x": 1000,
"value": 2
},
{
"x": 1000,
"value": 3
}
...
]
}
I am wondering how I can achieve result like this with jq:
[
{
"value": 1,
"from": "a",
},
{
"value": 2,
"from": "a"
},
...
{
"value": 1,
"from": "b"
},
{
"value": 2,
"from": "b"
}
...
]
Here is a very slightly generic, but hardly robust, solution:
map_values( if type == "array"
then map(if type == "object" then .value else . end)
else . end)
| [ keys_unsorted[] as $k
| .[$k][] as $v
| { value: $v, from: $k } ]
Create two lists, one from .a and one from .b, and merge them with +.
In the first list, create objects with value: set to the original content and add from: "a"; in the second list, remove .x from elements of .b and add the from again.
jq '[.a[] | {value:(.), from: "a"}] + [.b[] | del(.x) + {from: "b"}]

jq update list elements based on values

Is it possible to use jq to turn the following JSON data
[
{
"a": null,
"b": [
{
"c": "cc",
"d": "dd1"
},
{
"c": "cc",
"d": "dd1",
"e": "ee",
"f": "ff"
}
]
},
{
"b": [
{
"c": "cc",
"d": "dd2",
"e": "ee",
"f": "ff"
}
]
}
]
into
[
{
"a": null,
"b": [
[
"cc", "d1"
],
[
"cc", "d1", "ff"
]
]
},
{
"b": [
[
"cc", "d2", "ff"
]
]
}
]
?
Note that the purpose is to reduce the b list with certain elements of its items based on a condition. The condition assigns the string d1 if the value of d is dd1, otherwise d2 is assigned if dd2 is present.
The following unsuccessful attempt demonstrates the idea:
$ jq -r '.[].b[] = [.[].b[].c, ?, .[].b[].f?]'
This is probably not the best way to do what you want, but it does get the desired output and it has the conditional you asked about.
jq '.[].b |= map(
[ .c,
(if .d == "dd1" then "d1" elif .d == "dd2" then "d2" else . end),
.f // empty
] )'
Without changing the value of the d key, you would use this jq filter:
jq '.[].b[]|=(to_entries|map(.value))' file
That updates the b array into all values of the inner objects.
If you want to update the d, you could use this filter:
jq '.[].b[] |= (to_entries|
map(
if(.key=="d") then
.value|=sub("d+";"d")
else .
end
|.value)
)' file
that adds a check if the key is d the associated value is updated to remove the duplicated d character from the string. This requires jq to support for regex (to be able to use the sub function).

Modifying nested json conditionally using jq

I would like to modify something like the following JSON:
{
"foobar": {
"a": {
"adkjfe": {
"A": 1,
"foo": "bar"
}
},
"b": {
"ekjaei": {
"A": 2,
"bar": "foo"
}
}
}
}
to add more data say {"baz": ["bing", "bop"]} to the parent of A if A=1. Assuming I don't know the parent key while leaving the rest of the json untouched. I've tried many different things including:
.foobar | .. | .. | .[] | if select(.A==1) then . += {"baz": "bing"} else . end
which gives me an error and only my modified section.
The result, in this case, that I would like to see is:
{
"foobar": {
"a": {
"adkjfe": {
"A": 1,
"foo": "bar",
"baz": ["bing", "bop"]
}
},
"b": {
"ekjaei": {
"A": 2,
"bar": "foo"
}
}
}
}
Select only fields that are of type object and that match your condition (A == 1) :
jq '(.foobar | .. | select(type == "object" and .A == 1)) |= .+ {"baz": ["bing", "bop"]}' test.json
The () around the filter query take care that the whole document will be returned with the updated fields and not just your subdocument
In general it is better to avoid .. if possible, for reasons of efficiency. In the present case, the following will do the job:
(.foobar[][] | select(.A == 1)) |= .+ {"baz":["bing", "bop"]}