0

I have this JSON object:

{
            id: {
              name: "BBCommercialPropertyStructureFloodCoverage",
            },
            carrierData: null,
            link: [
              {
                key: "Location",
                select: {
                  key: "ARRAY_INDEX",
                  value: 0,
                },
              },
              {
                key: "structure",
                select: {
                  key: "ARRAY_INDEX",
                  value: 1,
                },
              },
              {
                key: "Coverage",
                select: {
                  key: "ARRAY_INDEX",
                  value: 0,
                },
              },
            ],
          },

I have several of these objects with different index values. For example, if I want to match on this id/name: "BBCommercialPropertyStructureFloodCoverage" and also that the location is value 0 and the structure is value 1, how do I do this using jmespath?

I have this so far:

const floodCoverageQuery = [?id.name=='BBCommercialPropertyStructureFloodCoverage' && link[key=='Location' && select.value==0]] && link[key=="structure" && select.value==1]]

Is this right?

Jwan622
  • 8,910
  • 11
  • 56
  • 125

1 Answers1

2

First of all, the object tree you put in your answer is not true JSON. I've converted it, see below. Also, I suppose the object needs to be wrapped in a list ([...]) because you say ”I have several of these objects“; and the jmespath query you have ”so far“ starts with [?..., which indicates you've got a list.

answer

this should work, I've tested it on jmespath.org:

[?id.name=='BBCommercialPropertyStructureFloodCoverage' && link[?key=='Location' && select.value==`0`] && link[?key=='structure' && select.value==`1`]]

what was wrong with your solution?

Your original query was this:

[?id.name=='BBCommercialPropertyStructureFloodCoverage' && link[key=='Location' && select.value==0]] && link[key=="structure" && select.value==1]]

This is what I had discovered:

  • There are too many closing brackets (]). So ==0]] && should be ==0] && (one bracket less).
  • You've mixed single and double quotes (' and "). Only single quotes are valid raw string literals. (Alteratively, you could write ⁠`"string"`⁠, which is equivalent to 'string'.)
  • Wrap integer values (0 and 1) inside backticks (⁠`0`⁠ and ⁠`1`⁠), see literal expressions.
  • The inner brackets, where you check key and select.value, are filter expressions, so you need to wrap them inside [? and ] instead of just [...].

FTR, the actual input as JSON

[
  {
    "id": {
      "name": "BBCommercialPropertyStructureFloodCoverage"
    },
    "carrierData": null,
    "link": [
      {
        "key": "Location",
        "select": {
          "key": "ARRAY_INDEX",
          "value": 0
        }
      },
      {
        "key": "structure",
        "select": {
          "key": "ARRAY_INDEX",
          "value": 1
        }
      },
      {
        "key": "Coverage",
        "select": {
          "key": "ARRAY_INDEX",
          "value": 0
        }
      }
    ]
  }
]
myrdd
  • 2,272
  • 1
  • 17
  • 22