Yash Rastogi
Yash Rastogi

Reputation: 475

Unable to use JSON file using python

{
  "id": "APA91bE9N6D9Tp79gv1kUgWLhsCmbKPKJQlzgtr1iGKlL5249bzD5DxySBiaIzDmk7rOAdrWcNcP0ZxPnaj7e6Esc _iGIYJlDte-E1pMO9GME4QufgdQQOIccM2tExMd9L9RsQthR3160KbQeRmtfxW6gvuPXYN0zw",
  "platform": "android",
  "user": ObjectId("545b2833b21e898413de9314"),
  "_id": ObjectId("545b5e76d6be01755625b284"),
  "createdAt": Date(1415274102856),
  "__v": 0
}{
  "__v": 0,
  "_id": ObjectId("545b67c4d6be01755625b2c1"),
  "createdAt": Date(1415276484321),
  "id": "APA91bFRxirYHIko33D1LiHODpBd77IlRhebK4tMRWecFxb5E6nfWSMFarr5mlwmY9bPQP56DGP7cnli4_jOrS8Ynn3Y9w9uaRoESoEPglqR-rA-3phsh8UtSxMC5lNoOqIrohz3hBjzzpCH_vExwo6B5yV6Mb8jyg",
  "platform": "android",
  "user": ObjectId("545b69a5d6be01755625b2d2")
}

Here is the content of the JSON File.

Code which i am using for importing is:

import json

with open("test.json") as json_file:
    json_data = json.load(json_file)
    print(json_data)  

Upvotes: 0

Views: 101

Answers (2)

Joachim Rosskopf
Joachim Rosskopf

Reputation: 1269

Your first string is not valid JSON. It is not possible to ingest this directly to JSON parser. What I would do, is to write a preprocessor, which expands the non JSON elements like ObjectId or Date to e.g. strings. Something in the line of this answer.

Upvotes: 1

vatsal mevada
vatsal mevada

Reputation: 5636

As @Puffin pointed out you need to handle ObjectId, Date etc if you are dumping a MongoDB BSON into JSON and accessing it.

If possible then use pymongo to access MongoDB directly from Python rather than dumping into JSON and accessing the data.

Upvotes: 1

Related Questions