James
James

Reputation: 167

JSON into Dictionary type in python

I have a set of following data :

{
    "dataFrame": "AB3hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 3, 
    "id": 1480528200533, 
    "port": 5, 
    "rssi": -116, 
    "sf_used": 10, 
    "snr": -8.5, 
    "timestamp": "2016-11-30T17:50:00.533Z"
}, 

{
    "dataFrame": "AB3hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 5, 
    "id": 1480528235613, 
    "port": 5, 
    "rssi": -119, 
    "sf_used": 10, 
    "snr": -5.8, 
    "timestamp": "2016-11-30T17:50:35.613Z"
}, 
{
    "dataFrame": "AB7hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 7, 
    "id": 1480528310609, 
    "port": 5, 
    "rssi": -120, 
    "sf_used": 10, 
    "snr": -8.8, 
    "timestamp": "2016-11-30T17:51:50.609Z"
}, 
{
    "dataFrame": "AB7hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 9, 
    "id": 1480528403504, 
    "port": 5, 
    "rssi": -116, 
    "sf_used": 10, 
    "snr": -9.2, 
    "timestamp": "2016-11-30T17:53:23.504Z"
}, 

I am trying to convert above into dictionary type :

{
    u'dataFrame': 'AB3hqqqpVVVOAAA=', 
    u'decrypted': true, 
    u'fcnt': 3, 
    u'id': 1480528200533, 
    u'port': 5, 
    u'rssi': -116, 
    u'sf_used': 10, 
    u'snr': -8.5, 
    u'timestamp': '2016-11-30T17:50:00.533Z'
}

So when I try to use infile = json.load(infile) where infile is my input file, why am i getting error like ValueError : extra data ?

Upvotes: 0

Views: 39

Answers (1)

Drathier
Drathier

Reputation: 14509

Because your input file is a number of json objects in a row, not a list of json objects. The parser will see the first , after the first object closes, and have no idea why that comma is there, so it raises a ValueError (which is actually a json syntax error).

Make the objects into a list by adding a single [ ] around them all like so:

[{
    "dataFrame": "AB3hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 3, 
    "id": 1480528200533, 
    "port": 5, 
    "rssi": -116, 
    "sf_used": 10, 
    "snr": -8.5, 
    "timestamp": "2016-11-30T17:50:00.533Z"
}, 

{
    "dataFrame": "AB3hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 5, 
    "id": 1480528235613, 
    "port": 5, 
    "rssi": -119, 
    "sf_used": 10, 
    "snr": -5.8, 
    "timestamp": "2016-11-30T17:50:35.613Z"
}, 
{
    "dataFrame": "AB7hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 7, 
    "id": 1480528310609, 
    "port": 5, 
    "rssi": -120, 
    "sf_used": 10, 
    "snr": -8.8, 
    "timestamp": "2016-11-30T17:51:50.609Z"
}, 
{
    "dataFrame": "AB7hqqqpVVVOAAA=", 
    "decrypted": true, 
    "fcnt": 9, 
    "id": 1480528403504, 
    "port": 5, 
    "rssi": -116, 
    "sf_used": 10, 
    "snr": -9.2, 
    "timestamp": "2016-11-30T17:53:23.504Z"
}]

Upvotes: 1

Related Questions