简体   繁体   中英

Parsing nested JSON in python?

I have a JSON file in following format

{
  "_id": {
    "$oid": "5458c00ceb23024b941be4bb"
  },
  "gpstime": 0.046575,
  "gpslat": 12.94492917,
  "readingtime": {
    "$date": "2014-11-04T17:28:10.000+0000"
  },
  "gpslong": 77.56115458,
  "deviceid": "11119828",
  "time": "Tue Nov  4 12:01:16 2014",
  "location": [
    12.94492917,
    77.56115458
  ]
}                                                                                                         

I used the following code to parse it but it is till missing out on date

import json
import csv
import pandas as pa

with open('readings.json', 'rb') as f:
    data = f.readlines()

data = map(lambda x: x.rstrip(), data)
data_json_str = "[" + ','.join(data) + "]"
data_df = pa.read_json(data_json_str)  

I get readingtime column as follows

readingtime  {u'$date': u'2014-11-04T17:27:50.000+0000'}                                                                 

But is missing out on %date in reading time how to fix this ?

The time field is correctly read, it correctly obtains its own column:

>>> print data_df.time
0    Tue Nov  4 12:01:16 2014
Name: time, dtype: object

Please note that neither $date nor time fields are intepreted , they are just loaded as strings.

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