0

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.

1
  • try $.groups[*].[sequence] Commented Oct 14, 2016 at 23:02

1 Answer 1

1

You do not need a variable sequence. When you need sequence, move item from one group to another, sort you can use array in object, and object in array.

Array use to sort, move items, change sequence. Objects to grouping of attributes within a single item

var groups = 
  [
    {"fields": [
        {name: 'field1'},
        {name: 'field2'},
        {name: 'field3'}
        ],
    },

    {"fields": [
        {name: 'field1'},
        {name: 'field2'},
        {name: 'field3'}
      ]
    },

    {"fields": [
        {name: 'field1'},
        {name: 'field2'},
        {name: 'field3'}
      ]
    }
  ];
console.log(groups[0].fields);            // Array [ Object, Object, Object ]
console.log(groups[2].fields[0].name);    // field1

Function splice is the best to change sequence. In same fields, and another fields Example for change sequence in same fields

var  oldIndex = 2, newIndex = 0, item = groups[0].fields.splice(oldIndex, 1)[0];
console.log(item);

groups[0].fields.splice(newIndex, 0, item);
console.log(groups);
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense, but the original question is, can I do this in a single JSON path statement? Sure, I can write JavaScript to get the information I need, but I'm trying to learn a new thing here, too. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.