How to extract data from json where parameter has white space character

262 views Asked by At

I have json

[
  {
    "name": "Name1",
    "address": "City1\nCountry1"
  },
  {
    "name": "Name2",
    "address": "City2 Country2"
  }
]

And I am using jayway library to extract "name" using "address". I want to parameterize the "address" using jpath

$.*[?(@.address=='{address}')].name

However, as you can see in json, first address has "\n" on it. Is there a way like normalize spacing for it so whenever I use jpath

$.*[?(@.address=='City1 Country1')].name

the value will still be extracted

1

There are 1 answers

0
wp78de On

A little late, a possible JSON-Path soltution is to use the contains operator for both parts:

$..[?(@.address contains 'City1' && @.address contains 'Country1')].name

However, this is not the same as a == comparison and could theoretically produce false positives.

Another option is to use the regex filter =~. Using it gives you full control and the ability to only match what you want; since you have a newline we need to inline (?s) to make .* match any character including newlines.

$..[?(@.address =~ /(?s)City1.*Country1/i)].name