'How to get data from two nodes with JMESPath
I get search results as JSON and try to match specific data with JMESPath . what succeed only in part.
JSON looks like this:
{
"search_parameters": {
"location": "Berlin,Berlin,Germany",
"q": "mykeyword"
},
"organic_results": [
{
"position": 1,
"domain": "www.example1.com"
},
{
"position": 2,
"domain": "www.example2.com"
}
]
}
With this JMESPath expression organic_results[].{POSITION:position,DOMAIN:domain}
I get this data matched:
[
{
"POSITION": 1,
"DOMAIN": "www.example1.com"
},
{
"POSITION": 2,
"DOMAIN": "www.example2.com"
}
]
My expected output is:
[
{
"QUERY": "mykeyword",
"POSITION": 1,
"DOMAIN": "www.example1.com"
},
{
"QUERY": "mykeyword",
"POSITION": 2,
"DOMAIN": "www.example2.com"
}
]
I tried to add to the working part KEYWORD:search_parameters.q
in any combination - but it just doesn't work for me.
What is the hint to get the output I need?
Edit: with {KEYWORD:search_parameters.q,POSITION:organic_results[0].position,DOMAIN:organic_results[0].domain}
I'm near the mark: my output is
{
"KEYWORD": "mykeyword",
"POSITION": 1,
"DOMAIN": "www.example1.com"
}
But how can I get all nodes without repeating this expression with [0], [1]
and so on? Using wildcard doesn't work for me.
Solution 1:[1]
To achive the goal I used finally this expression:
{QUERY:search_parameters.q,organic_results:organic_results[].{POSITION:position,DOMAIN:domain}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Evgeniy |