0

I was able to get a result from an API that returns a list but getting values from the list is seemingly difficult.

['[{"place_id":123018700,"licence":"Data', '©', 'OpenStreetMap', 'contributors,', 'ODbL', '1.0.', 'https://osm.org/copyright","osm_type":"way","osm_id":158053311,"boundingbox":["33.642992","33.6435452","-117.8422864","-117.8414719"],"lat":"33.64324045","lon":"-117.84185686276017","display_name":"Bren', 'Hall,', 'Ring', 'Mall,', 'University', 'of', 'California,', 'Irvine,', 'Irvine,', 'Orange', 'County,', 'California,', '92697,', 'United', 'States', 'of', 'America","class":"building","type":"yes","importance":0.511,"address":{"building":"Bren', 'Hall","road":"Ring', 'Mall","suburb":"University', 'of', 'California,', 'Irvine","city":"Irvine","county":"Orange', 'County","state":"California","postcode":"92697","country":"United', 'States', 'of', 'America","country_code":"us"},"svg":"M', '-117.8422864', '-33.6434792', 'L', '-117.8422046', '-33.6431755', '-117.8421809', '-33.6431799', '-117.8421303', '-33.642992', '-117.8415931', '-33.6430922', '-117.8415563', '-33.6431147', '-117.8414719', '-33.6431671', '-117.8415225', '-33.6433552', '-117.8418132', '-33.643301', '-117.8418253', '-33.643346', '-117.8418766', '-33.6433364', '-117.8419327', '-33.6435452', 'Z"}]']

This is the output from the API, I need to be able to take pieces of information of it out like "lat", "lon", and "display_name"

Tom Ron
  • 5,906
  • 3
  • 22
  • 38

1 Answers1

0

Are you sure you've copied the API response into your question correctly?

Because it looks like you have this sort of formatted response (very loose pseudo-code):

api_output: list(string(list(dict)))

My suggestion would be to

  • Access the elements of the list, in your case you only have one so you can index it with [0]
  • Parse the interior string which contains a list by using ast.literal_eval and from there access the dict inside that.

However I ask if you've copied it correctly because I attempted by above method and it doesn't work as the interior of the string is badly formed (example: "licence":"Data', '©', ) notice the lack of matching single quotes on Data.

If that is really the output then as you've identified, it might be relatively tricky. You could perform regex searches on the key's that you want, and search for a matching set of single/double quotes after your ${key}: Something along these lines: Javascript Regex to match value of JSON key value pair although obviously add the key of interest