I have a problem with 'jq' I could not solve after searching for a few hours. Let's take this simple JSON as an example with "Category4" nested within "Category2":
{
"categories": [
{
"Id": 1,
"Name": "Category1",
"Children": []
},
{
"Id": 2,
"Name": "Category2",
"Children": [
{
"Id": 4,
"Name": "Category4",
"Children": []
}
]
},
{
"Id": 3,
"Name": "Category3",
"Children": []
}
]
}
I would like to add a "Category5" child within the "Category4" object such as:
{
"categories": [
{
"Id": 1,
"Name": "Category1",
"Children": []
},
{
"Id": 2,
"Name": "Category2",
"Children": [
{
"Id": 4,
"Name": "Category4",
"Children": [
{
"Id": 5,
"Name": "Category5",
"Children": []
}
]
}
]
},
{
"Id": 3,
"Name": "Category3",
"Children": []
}
]
}
I can do it by using the full path of the "Category4" object with:
jq --argjson a '[{"Id":5,"Name":"Category5","Children":[]}]' '.categories[1].Children[0].Children += $a' "myfile.json"
But how can I achieve the same result if I don't know the position of "Category4" (which could be at root level or nested deep inside other objects)? This command was my best guess:
jq --argjson a '[{"Id":5,"Name":"Category5","Children":[]}]' '.. | select(.Id?==4) | .Children += $a' "myfile"
but it only retrieves "Category4" and "Category5" objects (Category1, 2 and 3 are missing from the output). I feel like I am missing something stupid...
Thanks for any help!
Use walk builtin for applying filters to values at arbitrary depths without changing the overall structure.
walk(select(.Id? == 4) .Children += $a)
demo at jqplay.org
Related
I have some JSON data that looks like this
[
{Key: "fruits/red/apple", Value: "Red apples"},
{Key:"fruits/green/lime", Value: "Green Limes"},
{Key: "fruits/blue/berries/blueberry", Value: "Blue Berries"},
{Key: "vegetables/red/tomato", Value: "Red Tomatoes"},
{Key: "vegetables/green/cucumber", Value: "Green Cucumbers"}
]
And I am trying to extract the data to a nested JSON-tree structure like
{
"fruits": {
"id": 1,
"name": "fruits",
"children": [
{
"id": 2,
"name": "red",
"path": 1.2,
"children": [ { "id": 3, "name": "apple", "path": 1.2.3 } ]
},
{
"id: 4,
"name": "green",
"path": 1.4,
"children": [ {"id": 5, "name": "lime", "path": 1.4.5} ]
},
{
"id: 6,
"name": "blue",
"path": 1.6,
"children": [ {"id": 7, "name": "berries", "path": 1.6.7, "children": [{...}] } ]
}
]
},
"vegetables": {...}
}
I am new to jq and have something like this that gives me one level of data, but am lost on how to do running counters and recursion
[ .[] | { name: .Key, description: .Value, children: ( [.Key | split("/")] | .[0] | to_entries ) } ]
appreciate any pointers on this.
The desired output is not JSON, and it would be difficult to produce those non-numeric paths (e.g. 1.2.3). You could obviously add quotation marks to make them strings, but it would be much better to choose a more standard or convenient path representation.
Other than that, you can rest assured that jq is up to the task, though it would require some expertise in programming generally, or at least fluency with jq.
I'm trying to prune nodes deeply within a JSON structure and I'm puzzled why empty behaves seemingly different from a normal value here.
Input
[
{
"name": "foo",
"children": [{
"name": "foo.0",
"color": "red"
}]
},
{
"name": "bar",
"children": [{
"name": "bar.0",
"color": "green"
},
{
"name": "bar.1"
}]
},
{
"name": "baz",
"children": [{
"name": "baz.0"
},
{
"name": "baz.1"
}]
}
]
Program
jq '(.[].children|.[])|=if has("color") then . else empty end' foo.json
Actual output
[
{
"name": "foo",
"children": [
{
"name": "foo.0",
"color": "red"
}
]
},
{
"name": "bar",
"children": [
{
"name": "bar.0",
"color": "green"
}
]
},
{
"name": "baz",
"children": [
{
"name": "baz.1"
}
]
}
]
Expected output
The output I get, except without the baz.1 child, as that one doesn't have a color.
Question
Apart from the right solution, I'm also curious why replacing empty in the script by a regular value like 42 would replace the children without colors with 42 as expected, but when replacing with empty, it looks like the else branch doesn't get executed?
.[].children |= map(select(.color))
Will remove children that does not has an color so the output becomes:
[
{
"name": "foo",
"children": [
{
"name": "foo.0",
"color": "red"
}
]
},
{
"name": "bar",
"children": [
{
"name": "bar.0",
"color": "green"
}
]
},
{
"name": "baz",
"children": []
}
]
Online demo
Regarding why your filter does not seem to like empty;
This git issue seems to be the cause, multiple elements with empty will fail.
There must be a bug with assigning empty to multiple paths.
In this case you can use del instead:
del(.[].children[] | select(has("color") | not))
Online demo
I'd like to select/identity-output all objects in arrays under "emp" keys into a flat array of those objects.
[
{
"eng": {
"dev": {
"dir": {
"name": "Mickey"
},
"emp": [
{
"name": "Goofy",
"job": "laugh",
"start": "today"
},
{
"name": "Minnie",
"job": "laugh"
}
]
}
}
},
{
"mgmt": {
"dir": {
"name": "Donald"
},
"emp": [
{
"name": "Woody",
"job": "smile"
},
{
"name": "Buzz",
"job": "smile"
}
]
}
}
]
I'm looking for a flat array of arbitrary objects found in arbitrary locations within the document (in this example, under "emp" parent/keys).
In this example, it would look like
[
{
"name": "Goofy",
"job": "laugh",
"start": "today"
},
{
"name": "Minnie",
"job": "laugh"
},
{
"name": "Woody",
"job": "smile"
},
{
"name": "Buzz",
"job": "smile"
}
]
I've looked through a lot of documentation and am able to do this if I know in advance precisely where these 'emp' keys are in the document, but not if they're distributed through the document at a priori unknown locations/paths.
Use recurse to walk the structure. From all the substrucures, select objects with the emp key. Output the corresponding values and merge the resulting arrays.
jq '[recurse | select (type == "object" and .emp) | .emp ] | add' file.json
I have been playing around with jq to format a json file but I am having some issues trying to solve a particular transformation. Given a test.json file in this format:
[
{
"name": "A", // This would be the first key
"number": 1,
"type": "apple",
"city": "NYC" // This would be the second key
},
{
"name": "A",
"number": "5",
"type": "apple",
"city": "LA"
},
{
"name": "A",
"number": 2,
"type": "apple",
"city": "NYC"
},
{
"name": "B",
"number": 3,
"type": "apple",
"city": "NYC"
}
]
I was wondering, how can I format it this way using jq?
[
{
"key": "A",
"values": [
{
"key": "NYC",
"values": [
{
"number": 1,
"type": "a"
},
{
"number": 2,
"type": "b"
}
]
},
{
"key": "LA",
"values": [
{
"number": 5,
"type": "b"
}
]
}
]
},
{
"key": "B",
"values": [
{
"key": "NYC",
"values": [
{
"number": 3,
"type": "apple"
}
]
}
]
}
]
I have followed this thread Using jq, convert array of name/value pairs to object with named keys and tried to group the json using this expression
jq '. | group_by(.name) | group_by(.city) ' ./test.json
but I have not been able to add the keys in the output.
You'll want to group the items at the different levels and building out your result objects as you want.
group_by(.name) | map({
key: .[0].name,
values: (group_by(.city) | map({
key: .[0].city,
values: map({number,type})
}))
})
Just keep in mind that group_by/1 yields groups in a sorted order. You'll probably want an implementation that preserves that order.
def group_by_unsorted(key_selector):
reduce .[] as $i ({};
.["\($i|key_selector)"] += [$i]
)|[.[]];
I'm using scala play and am attempting to traverse a json tree in order to validate that specific name values have specific children with specific name values. I have the following Json in the form of a JsObject:
{ "name": "user", "children": [ { "name": "$a", "children": [ { "name": "foo", "children": [ ] }, { "name": "fooBar", "children": [ { "name": "$a", "children": [ { "name": "subFoobar1", "children": [ ] }, { "name": "subFoobar2", "children": [ { "name": "TEST", "children": [ ] } ] }, { "name": "subFoobar3", "children": [ ] } ] } ] }, { "name": "bar", "children": [ { "name": "$a", "children": [ ] }, { "name": "$c", "children": [ ] }, { "name": "$b", "children": [ ] } ] }, { "name": "barFoo", "children": [ ] } ] } ] }
Ideally I would use nested for loops to traverse but the JsObject structure is preventing me from accessing the underlying values when attempting traverse. I have also attempted mapping the JsObject to a map of type [Map[String,Map[String,Any]]] but I am getting invalid cast compiler errors.
Any tips on how I can traverse and validate the name value at each level would be appreciated. I would preferably like to use the play json library
Issue was in the case class I was attempting to use. I wasn't accounting for the recursive nature of my Json structure
case class ActorTree(name : String, children:Seq[ActorTree] )