5

I have a JSON and I need to select the value of the persons roles using JMESPATH. I can do it with only one and static id, like roles[?id=='1324'], but I can't do with a list of roles ids and selected from the JSON. I tried to do that with: roles[?id==person[ * ].role[ * ].id[ ]] and using pipe, but without any result. Below an example of my JSON.

{
   "person":[
      {
         "id":"999999999",
         "role":[
            {
               "id":"1324",
               "name":"Test"
            },
            {
               "id":"1578",
               "name":"Test 2"
            }
         ]
      },
      {
         "id":"888888888",
         "role":[
            {
               "id":"1234",
               "name":"Test"
            },
            {
               "id":"1678",
               "name":"Test 2"
            }
         ]
      }
   ],
   "roles":[
      {
         "id":"1234",
         "value":"$945.00"
      },
      {
         "id":"1324",
         "value":"$1245.00"
      },
      {
         "id":"1578",
         "value":"$3245.00"
      },
      {
         "id":"1678",
         "value":"$4245.00"
      }
   ]
}
1
  • You can't do it in JMES Path use jq. It's impossible because once you're in a node you can't go back, here you enter in the roles node and then you try to search something in the person node, but once you get in the roles node JMES Path ignore the other node so in your filter your query will never get any results. And more than that, I think you can't use any query in the filter, I don't know why. Commented Jul 22, 2019 at 14:04

1 Answer 1

2

Quick Answer (TL;DR)

  • Usually it is easy to painlessly query JSON with JMESPath
    • One pain-free key is to utilize JSON structures that are specifically normalized for optimal use with JMESPath
    • One pain-free key is knowing when to use dictionary (aka objects // associative-arrays // mappings) name-value pairs in your JSON to make all parts of the JSON capable of unambiguous reference
  • Unfortunately, the goal in this specific question is not doable with standard JMESPath, because JMESPath lacks a token to refer to the JSON data root in the current-node context
  • To accomplish this task, you will have to break out of JMESPath and use the functionality of the hosting language

Detailed Answer

Context

  • JMESPath v 0.9.4

Scenario:

  • DeveloperSOHugoFerreira wishes to query JSON data using a cross-reference from one part of the JSON to dynamically populate a JMESPath query on another part of the JSON

Problem

  • Unfortunately, the goal in this specific question is not doable with standard JMESPath, because JMESPath lacks a token to refer to the JSON data root in the current-node context

See also

Sign up to request clarification or add additional context in comments.

Comments

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.