Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native way to run a forEach loop on an unnamed JSON object

Tags:

jmespath

Example JSON

[
  {
   "id": 12,
   "clientName": "super-client"
  },
  {
   "id": 15,
   "clientName": "helloClient"
  }
]

I want to use JMESPath to check if each clientName field does not contain the text super- and then display the result

I'm using the API connector tool for Google Sheets to parse a JSON I get via a call. I'm stuck with this.

Almost all the reference examples on JMESPath site have a named object or array. This JSON I have doesn't have any named objects.

I want to do something like

result = []
for entry in data:
if not entry['clientName'].find("super-"):
result.append(entry)
return result
like image 566
Shads Avatar asked Nov 18 '25 20:11

Shads


1 Answers

In JMESPath, it is called a filter projection and it can be applied on an array right away, without it having to be named.

What you will need on top of a filter projection is the contains function and the not expression !.

All this together gives you this query:

[?!contains(clientName, 'super-')]

Which, on your JSON example will give, as a result:

[
  {
    "id": 15,
    "clientName": "helloClient"
  }
]
like image 120
β.εηοιτ.βε Avatar answered Nov 21 '25 14:11

β.εηοιτ.βε