Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonPath : filter by value in array

I'm trying to filter by value an array in my Json with Jsonpath. I want to get the long_name of the country in the JSON below. In order to do that, I filter the adress_components by types[0] == "country" but it doesn't seem to work.

The JsonPath I tried :

$.results[0].address_components[?(@['types'][0]=="country")].long_name

The result I want is : "Canada".

The JSON :

{
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "5510-5520",
                   "short_name" : "5510-5520",
                   "types" : [ "street_number" ]
                },
                {
                   "long_name" : "Yonge Street",
                   "short_name" : "Yonge St",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Willowdale",
                   "short_name" : "Willowdale",
                   "types" : [ "neighborhood", "political" ]
                },
                {
                   "long_name" : "North York",
                   "short_name" : "North York",
                   "types" : [ "political", "sublocality", "sublocality_level_1" ]
                },
                {
                   "long_name" : "Toronto",
                   "short_name" : "Toronto",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Toronto Division",
                   "short_name" : "Toronto Division",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Ontario",
                   "short_name" : "ON",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "Canada",
                   "short_name" : "CA",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "M2N 5S3",
                   "short_name" : "M2N 5S3",
                   "types" : [ "postal_code" ]
                }
             ]
            }
       ],
       "status" : "OK"
}

Thank you for your help.

like image 387
ShenM Avatar asked Nov 30 '17 14:11

ShenM


People also ask

How do I specify JsonPath?

A JSONPath expression begins with the dollar sign ( $ ), which represents the root element. An expression is formed by following this up with a sequence of child elements, separated by a period ( . ), called the dot notation or square brackets ([ '...' ]) called the bracket notation.

What is Jayway JsonPath?

Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.

What is the array slice operator in JsonPath in Rest assured?

: operator is the array slice operator, so you can slice collections using the syntax [start:end:step] to return a subcollection of a collection. ( ) operator lets you pass a script expression in the underlying implementation's script language. It's not supported by every implementation of JSONPath, however. ?

What is JSON Path Finder?

This tool will help you find all available JSONPaths in a JSON document, view them in an object tree, identify the different input expressions and extract the matching piece of JSON data against the input expression.


2 Answers

The following JSONPath will work:

$..address_components[?(@.types[0] == 'country')].long_name

Breaking it down:

  • $..address_components: focus on the address_components array
  • [?(@.types[0] == 'country')]: find the address_components sub document having a type attribute named "type" containing an array of which the first value is "country"
  • .long_name: return the long_name attribute of this sub document.

Verified using the Jayway JsonPath Evaluator and in Java:

JSONArray country = JsonPath.parse(json)
    .read("$..address_components[?(@.types[0] == 'country')].long_name");

// prints Canada
System.out.println(country.get(0));
like image 157
glytching Avatar answered Oct 05 '22 07:10

glytching


The working solution offered by glytching won't anymore if country is not the first one of the types array.

You should rather use:

$..address_components[?(@.types.indexOf('country') != -1)]

It will filter by array contains country, rather than array starts with country

like image 44
Charlie Avatar answered Oct 05 '22 06:10

Charlie