简体   繁体   中英

Extract values from JSON in Python

This is a reply from the Google Distance Matrix. I would like to have only the two value's as an output and I'm trying to extract it with Python.

{
   "destination_addresses" : [ "Hoorn, Nederland" ],
   "origin_addresses" : [ "Amsterdam, Nederland" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "45,0 km",
                  "value" : 44952
               },
               "duration" : {
                  "text" : "40 min.",
                  "value" : 2423
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

This is what I've tried so far. I want to have a reply from Google every 15 minutes and write it to a file. But in the file I only want the value for distance and duration and I have no luck in achieving that. I'm not sure if the reply from Google is proper JSON so I've tried json.loads but it did not allow me to extract certain parts from the reply. Any ideas?

import requests
import json
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

uri = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Amsterdam&destinations=Hoorn&mode=driving&key="
now = datetime.datetime.now()

def job():
    datum = now.strftime("%Y-%m-%d %H:%M")
    print datum
    text_file = open('tijdsduur.txt', 'a')
    batch = requests.get(uri)
    data = batch.text
    jdata = json.loads(data)
    print jdata['elements']['distance']['value']
    text_file.write(data)
    text_file.write('\n')
    text_file.close()

job()
scheduler = BlockingScheduler()
scheduler.add_job(job, 'interval', minutes=15)
scheduler.start()

Error when I try json.loads: KeyError 'elements'

json.loads will take a string paramater. The 's' in the loads is for string.

import json
a="""{
   "destination_addresses" : [ "Hoorn, Nederland" ],
   "origin_addresses" : [ "Amsterdam, Nederland" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "45,0 km",
                  "value" : 44952
               },
               "duration" : {
                  "text" : "40 min.",
                  "value" : 2423
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}"""

b = json.loads(a)
dist,dur = b['rows'][0]['elements'][0]['distance'], b['rows'][0]['elements'][0]['duration']
print dist
print dur

Output:

{u'text': u'40 min.', u'value': 2423}
{u'text': u'45,0 km', u'value': 44952}

Note that:

  • str(json_object) wraps strings in single quotes, which is not valid JSON.
  • So if you have a json object which you had earlier converted to string using str() and now you want to convert it back to JSON then you can work around like this:

     json.loads(json.dumps(str(json_object))) 

i think your problem because \\n character. try to remove it and use json.loads to extract the response in batch to be json. Here is the response that i got from google:

u'{\n   "destination_addresses" : [ "Hoorn, Netherlands" ],\n   "origin_addresses" : [ "Amsterdam, Netherlands" ],\n   "rows" : [\n      {\n         "elements" : [\n            {\n               "distance" : {\n                  "text" : "45.0 km",\n                  "value" : 44952\n               },\n               "duration" : {\n                  "text" : "40 mins",\n                  "value" : 2423\n               },\n               "status" : "OK"\n            }\n         ]\n      }\n   ],\n   "status" : "OK"\n}\n'


batch.replace("\n", "")
jsonData = json.loads(batch)

Now you have jsonData as JSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM