I'm new to JSON Path, so this may be simpler than I expect, if it's even possible.
I have a JSON block that consists of a set of groups, with each group having a set of fields. Each group also has a sequence, as does each field. The field sequence represents order within a group, and the group sequence represents the display order of the group itself.
I generated this programmatically in C# off of a flat list of items (groups and fields) with different sequence values, so I want to validate my output and, by extension, my grouping algorithm.
The JSON block looks (vastly simplified) like this:
{
"groups": [
{
"sequence": 0,
"fields": [
{
"sequence": 0
},
{
"sequence": 1
},
{
"sequence": 2
}
]
},
{
"sequence": 1,
"fields": [
{
"sequence": 0
},
{
"sequence": 1
}
]
},
{
"sequence": 2,
"fields": [
{
"sequence": 0
},
{
"sequence": 1
},
{
"sequence": 2
}
]
}
]
}
I'm trying to validate this with JSON Path. For this, I don't have a tool, so I'm using the jsonpath online evaluator.
What I'm aiming for is output along the lines of this:
'0'
'sequence' => '0'
'fields'...
'0'
'sequence' => '0'
'1'
'sequence' => '1'
'2'
'sequence' => '2'
// etc...
In other words, I'm looking for a JSON path query that can return the sequence of each group plus the sequence of each field in the group, in a hierarchy.
To get the sequence of the groups, I'm using the following. This works fine, and gives me useful output, since groups are the top-level item already:
$.groups[*].sequence
Output:
'0' => "0"
'1' => "1"
'2' => "2"
To get the sequence of the fields in the groups, I'm using the following. The output here is less useful, as it's a flat list that can get difficult to read if I have dozens or more fields spread out across several groups.
$.groups[*].fields[*].sequence
Output:
'0' => "0"
'1' => "1"
'2' => "2"
'3' => "0"
'4' => "1"
'5' => "0"
'6' => "1"
'7' => "2"
So ultimately, the question is this: is there a JSON path query that will let me get the information I need here hierarchically? Or am I using the wrong tool for the job? I suppose I could write a few lines of code in JavaScript that will let me do this if I have to, but it seems like JSON path might be a powerful tool to learn, so if I can do it here I've learned something.
$.groups[*].[sequence]