0

I went through most of the Posts here regarding python parsing multiple json objects in a file, But most of them answer with objects in are line.

my json file

{
    "KeyPairs": [
        {
            "KeyName": "awschrome", 
            "KeyFingerprint": "f7:cf:a0:54:02:e3:b6:eb:07:05:5d:0a:3e:f9:37:15:06:9e:f1:95"
        }, 
        {
            "KeyName": "dhepal", 
            "KeyFingerprint": "ba:61:ed:48:6b:bc:cb:08:2f:0f:ec:3d:c9:65:a4:0d:75:46:b5:6f"
        }, 
        {
            "KeyName": "load", 
            "KeyFingerprint": "c3:2d:b9:df:94:5c:93:88:4b:53:1c:e5:49:40:f6:9f:33:cb:9a:12"
        }, 
        {
            "KeyName": "pal", 
            "KeyFingerprint": "ac:a6:e9:c5:be:ea:51:2b:25:41:6d:e3:72:ed:5b:87:cd:a2:80:80"
        }, 
        {
            "KeyName": "terraform_new", 
            "KeyFingerprint": "94:37:be:54:a2:82:17:84:bc:37:c7:68:2a:41:b6:2f:5d:f3:79:69"
        }, 
        {
            "KeyName": "vicky2", 
            "KeyFingerprint": "5b:b9:27:20:c7:fd:c8:09:02:70:d2:5a:95:a3:69:a0:be:fb:17:db"
        }
    ]
}
{
    "Tags": [
        {
            "ResourceType": "snapshot", 
            "ResourceId": "snap-01a5447df18a68d40", 
            "Value": "python", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "instance", 
            "ResourceId": "i-017b6d851e312d83a", 
            "Value": "Terraform_new", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "instance", 
            "ResourceId": "i-063affcbad7bae443", 
            "Value": "Terraform", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "instance", 
            "ResourceId": "i-096c54c0c00340ef9", 
            "Value": "Debian", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "network-interface", 
            "ResourceId": "eni-486c4214", 
            "Value": "dhepal", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "volume", 
            "ResourceId": "vol-04451515102b0bb4c", 
            "Value": "Terraform_new", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "volume", 
            "ResourceId": "vol-0a0031d456489d18d", 
            "Value": "Terraform", 
            "Key": "Name"
        }, 
        {
            "ResourceType": "volume", 
            "ResourceId": "vol-0b14e811e9c003744", 
            "Value": "Debian", 
            "Key": "Name"
        }
    ]
}

my code:

import sys, json, xlwt

with open('json_data_file','r')as f:
        data =f.read()

print data
parsed_json=json.loads(data)

print parsed_json

i know that the above code doesnt work to read multiple json object and it provides error

File "./xlsconversion.py", line 9, in <module>
    parsed_json=json.loads(data)
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 29 column 1 - line 82 column 1 (char 919 - 2335)

Loading and parsing a JSON file with multiple JSON objects in Python

How do I use the 'json' module to read in one JSON object at a time?

the links above give me an insight about the multiple json objects but i didnt get a way out to read my json file. I'm a beginner to python and please suggest me how i can work through to get read multiple json objects and a single json object using python. NOTE: I am using python2.7

Ravi Kumar
  • 13
  • 1
  • 1
  • 7
  • The files contains 2 json objects so that's why json.loads does not work.. you would need to parse it object by object or add [ obj,1 obj2] this would make it a list of dictionaries. The fact that an object is not on a one line is not a problem – Sedy Vlk Aug 23 '17 at 08:43
  • solution available is to parse json object which are present in a line and not for the json object format i have posted. please can you help me how multiple json and single json can be parsed with same code. – Ravi Kumar Aug 23 '17 at 10:03

0 Answers0