3

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
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Shads
  • 33
  • 2

2 Answers2

2

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"
  }
]
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thank you! this works and yours is cleaner than what i did. So as an extension of this, how do I add multiple search strings in the contains function? I tried the following `[?![contains(clientName, 'super-') || contains(clientName, 'Man')]]` But i'm getting a blank worksheet with this. Where have I gone wrong in the syntax? For the purpose of this additional question, assume there's another object with `clientName = "SpiderMan` – Shads Mar 31 '22 at 18:18
  • No need to have an extra square bracket there: `[?!contains(clientName, 'super-') || contains(clientName, 'Man')]` – β.εηοιτ.βε Mar 31 '22 at 18:23
0

Try

function test() {
  var json = [
    {
      "id": 12,
      "clientName": "super-client",
    },
    {
      "id": 15,
      "clientName": "helloClient",
    }
  ]
  var result = json.filter(e => e.clientName.includes('super'))
  console.log(result)
}

  var json = [
    {
      "id": 12,
      "clientName": "super-client",
    },
    {
      "id": 15,
      "clientName": "helloClient",
    }
  ]
  var result = json.filter(e => e.clientName.includes('super'))
  console.log(result)
Mike Steelson
  • 14,650
  • 2
  • 5
  • 20