I have deeply nested data that looks like the following. The data may look needlessly nested, but I've removed unimportant data to reduce some clutter.
[
{
"id": "1",
"entries": [
{
"data": {
"values": [
{
"data_of_interest": {
"frames": [
{
"filename": "arg",
"module": "bar",
"package": "foo",
"platform": "blargh"
},
{
"filename": "arg1",
"module": "barge",
"package": "something",
"platform": "blargh.io"
}
]
}
}
]
}
}
]
},
{
"id": "2",
"entries": [
{
"data": {
"values": [
{
"data_of_interest": {
"frames": [
{
"filename": "app",
"module": null,
"package": null,
"platform": null
}
]
}
}
]
}
}
]
}
]
What I want to do is distribute the 'id' field into each record in the 'frames' array, as well as the index of each frame, then flatten out all the frames into a single list. The resulting data would look like this:
[
{
"id": 1,
"frame_idx": 0,
"filename": "arg",
"module": "bar",
"package": "foo",
"platform": "blargh"
},
{
"id": 1,
"frame_idx": 1,
"filename": "arg1",
"module": "barge",
"package": "something",
"platform": "blargh.io"
},
{
"id": 2,
"frame_idx": 0,
"filename": "app",
"module": null,
"package": null,
"platform": null
}
]
For the life of me, I can't figure out how to properly distribute the id or index into the frame records. My attempts always place id and index beside, but not within the frame records.
CodePudding user response:
Start by deep diving upto the innermost level(level of indexes of the "frames" list
). Then, determine the common factor(@(9,id)
) in order to nest the result under common arrays while seperating by [&1]
such as
[
{
"operation": "shift",
"spec": {
"*": { // top level indexes
"entries": {
"*": { //indexes of "entries"
"*": { // "data"
"*": { // "values"
"*": { // indexes of "values"
"*": { // "data_of_interest"
"*": { // "frames"
"*": { // indexes of "frames"
"@(8,id)": "@(9,id).[&1].id", // go 8 or 9 levels up respectively in order to grab the value of "id"
"$": "@(9,id).[&1].frame_idx",
"*": "@(9,id).[&1].&"
}
}
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]