Python: parse/extract json returned from locator service?

379
2
03-21-2019 10:36 AM
AllenScully
Occasional Contributor II

In a python script to be published as a GP service, I'm making a call to a Locator Service, getting results back - plan is to use the X & Y values from the locator return as input variables for some intersects etc. as part of the overall tool.  

The call/return from the locator is working, now I'm investigating the python json library to figure out how the extract just X & Y values into variables.  

My question is about whether/how you can use the json library to parse through the json and return the 2 attributes (the X and Y in my case) so they can be plugged into python variables..  

Results example:

{u'candidates': [{u'attributes': {u'Y': 446768.471952, u'X': 992202.058741}, u'score': 100, u'location': {u'y': 446768.47195232206, u'x': 992202.0587409921}, u'address': u'201 N STONE AV, TUCSON'}], u'spatialReference': {u'wkid': 2868, u'latestWkid': 2868}}

Alternately, after simply converting the Results to a string:

{
"spatialReference": {
"wkid": 2868,
"latestWkid": 2868
},
"candidates": [
{
"address": "201 N STONE AV, TUCSON",
"location": {
"x": 992202.0587409921,
"y": 446768.47195232206
},
"score": 100,
"attributes": {
"X": 992202.05874100002,
"Y": 446768.47195199999
}
}
]
}

Anyone been through this before?

Thanks - 

Allen

0 Kudos
2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Looks like dictionaries, extract the 'candidates' you have one candidate so a simple list slice [0] which gives you the remaining bits

d = {u'candidates': [{u'attributes': {u'Y': 446768.471952, u'X': 992202.058741},
     u'score': 100,
     u'location': {u'y': 446768.47195232206, u'x': 992202.0587409921},
     u'address': u'201 N STONE AV, TUCSON'}],
     u'spatialReference': {u'wkid': 2868, u'latestWkid': 2868}}

d['candidates'][0]['attributes']
Out[7]: {'Y': 446768.471952, 'X': 992202.058741}

d['candidates'][0]['attributes']['Y']
Out[8]: 446768.471952‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I did find this nifty dictionary unpacking function though

def extract_nested_values(it):
    if isinstance(it, list):
        for sub_it in it:
            yield from extract_nested_values(sub_it)
    elif isinstance(it, dict):
        for value in it.values():
            yield from extract_nested_values(value)
    else:
        yield it

print(list(extract_nested_values(d)))

[446768.471952, 992202.058741, 100, 446768.47195232206, 992202.0587409921, '201 N STONE AV, TUCSON', 2868, 2868]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You only have one row of data, so that is why the print is only one row long

It returns a generator so you convert it to a list and assign to a variable if needed

result = list(extract_nested_values(d))‍‍

one of loads of references

python - Cleaner way to unpack nested dictionaries - Stack Overflow 

forestknutsen1
MVP Regular Contributor

You can move freely between json and a python dict.

Converting Dictionary to JSON in python - Stack Overflow