I have JSON structure that is a nested parent/child kind of structure:

{
  "type": "site",
  "profile_id": "site profile id",
  "children": [
    {
      "type": "dealer",
      "profile_id": "dealer profile id",
      "children": [
        {
          "type": "location",
          "profile_id": "location profile id",
          "children": [
            {
              "type": "customer",
              "profile_id": "customer profile id",
              "children": [
                {
                  "type": "farm",
                  "farm_id": "farm id",
                  "children": [
                    {
                      "type": "field",
                      "field_id": "field id"
                     }]}]}]}]}]}

Is there some way in JSONPath to do one of the following:

  1. give me the profile_id if it's present, or give me the farm_id if it's present, or give me the field_id if it's present.

  2. give me the profile_id if type=customer, or give me the farm_id, if type=farm, or give me the field_id if type=field

  3. give me the nth attribute from each class. The id is actually the third attribute in each class. This is my least favorite option, because I don't know if the id will always be the third attribute.

1

There are 1 answers

0
Duncan On

A solution for option 2 that requires 3 separate queries would be the following. Here are the 3 queries:

$..[?(@.type=='customer')].profile_id
$..[?(@.type=='farm')].farm_id
$..[?(@.type=='field')].field_id

I verified these queries with http://www.jsonquerytool.com

Also see this question for a similar example.