0

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.

Evgeniy
  • 2,337
  • 2
  • 28
  • 68
  • For this, you will need a reference to the parent node when down in the array `organic_results`, which is not possible in JMESPath, see https://stackoverflow.com/questions/52683015/how-to-get-list-of-all-child-elements-with-field-from-parent – β.εηοιτ.βε May 13 '22 at 17:52

1 Answers1

0

To achive the goal I used finally this expression:

{QUERY:search_parameters.q,organic_results:organic_results[].{POSITION:position,DOMAIN:domain}}
Evgeniy
  • 2,337
  • 2
  • 28
  • 68