You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Due to some limitations with the application we're using, I need to sort of flatten this JSON (this is a third-party off the shelf application that only accepts a certain JSON format, but luckily the vendor implemented a JSLT solution to do so), so that it looks like this:
[
{
"a" : b,
"c" : d
},
{
"w" : x,
"y" : z
}
]
So, using the example from earlier, the expected output would be this:
I found a solution that works, but it's a bit clunky:
let thing_name = (.thing_name)
let thing_uuid = (.thing_uuid)
let thing_id = (.thing_id)
[
for (.child_stuff) {
"thing_name": $thing_name,
"thing_uuid": $thing_uuid,
"thing_id": $thing_id,
"name": .name,
"group": .group,
"sequence": .sequence,
"is_included": .is_included,
"dependent_on": .dependent_on,
"modifier": .modifier
}
]
Is there a way to do this more programmatically without having to statically define fields? Ideally I'd be able to use a nested for loop to first loop through the "child_stuff" array and then for each array item, loop through the three root fields and insert those into each child array item. That way if the fields change, I don't have to keep updating them and it "just works". Something like this:
let rootObj = (.)
[
for(.child_stuff)
{
.key : .value,
for($rootObj)
if (not(is-array(.key)))
.key : .value
}
]
I did find a StackOverflow question that seemed to indicate that you can't pull parent values into a child item and the parent items need to be defined with let statements, which is fine for my purposes. I was trying to do something like this but I can't get the syntax right:
let baseObj = {
"thing_name" : (.thing_name),
"thing_uuid" : (.thing_uuid),
"thing_id" : (.thing_id)
}
[
for (.child_stuff)
{
.key : .value,
[
for ($baseObj)
.key : .value
]
}
]
I tried looking in the documentation, but I don't see anything that specifically mentions this, and searching forums doesn't seem to have a generic enough example for me to understand. Any help would be greatly appreciated.
The text was updated successfully, but these errors were encountered:
let root_stuff = [ for (.) . if (not(is-array(.value)) and not(is-object(.value))) ]
[
for (.child_stuff) {
for ([array(.), $root_stuff] | flatten(.)) .key : .value
}
]
Hello,
I have a JSON response from an API call, like this (edited for brevity and obfuscation purposes):
Due to some limitations with the application we're using, I need to sort of flatten this JSON (this is a third-party off the shelf application that only accepts a certain JSON format, but luckily the vendor implemented a JSLT solution to do so), so that it looks like this:
So, using the example from earlier, the expected output would be this:
I found a solution that works, but it's a bit clunky:
Is there a way to do this more programmatically without having to statically define fields? Ideally I'd be able to use a nested for loop to first loop through the "child_stuff" array and then for each array item, loop through the three root fields and insert those into each child array item. That way if the fields change, I don't have to keep updating them and it "just works". Something like this:
I did find a StackOverflow question that seemed to indicate that you can't pull parent values into a child item and the parent items need to be defined with let statements, which is fine for my purposes. I was trying to do something like this but I can't get the syntax right:
I tried looking in the documentation, but I don't see anything that specifically mentions this, and searching forums doesn't seem to have a generic enough example for me to understand. Any help would be greatly appreciated.
The text was updated successfully, but these errors were encountered: