How do I write Json path expression for my payload

Here is my payload and I need to extract short_name of country & state

$.results[0].address_components[what goes here to extract].short_name

The objects are not consistent.

{"long_name":"San Francisco County","short_name":"San Francisco County","types":["administrative_area_level_2","political"]},{"long_name":"California","short_name":"CA","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},
0 4 775
4 REPLIES 4

Provide completed JSON example.

You didn't provide enough information for what you want. Also you didn't provide the actual json, or even the structure of the actual json, so I am guessing here.

To answer your question, "What goes here to extract" is known as a predicate, in the parlance of jsonpath. What predicate you use depends on what you want to do.

I'll give you one example and then let you figure it out.


Assuming the actual json is this

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "San Francisco County",
                    "short_name": "San Francisco County",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "California",
                    "short_name": "CA",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ]
        }
    ]
}


Maybe try something like this

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

For more on predicates, google jsonpath predicate.

Also; the jsonpath in Apigee is somewhat older. It may not support the latest jsonpath syntax and features. So you may need to resort to using a java callout in order to get what you want.

@Dino-at-Google Sorry about not providing complete json but it looks same like what you assumed. Thanks for your answer but I'm worried about using absolute values in this condition.

what if country is 1 and political is 0 in another response.

Would it not be better if we can just check "country" exists in the types array?

Maybe! I don't know the syntax of the jsonpath predicates. You can consult the doc link I provided to see if you can express what you want in the predicate.

If not I guess you'll need to resort to a JavaScript callout to accomplish what you want.