Sanjib Acharya
Sanjib Acharya

Reputation: 13

how to read json file with a list of strings in python

Here is a sample of json data I created from defaultdict in python.

[{
    "company": [
        "ABCD"
    ],
    "fullname": [
        "Bruce Lamont",
        "Ariel Zilist",
        "Bruce Lamont",
        "Bobby Ramirez"
    ],
    "position": [
        " The Hesh",
        " Server",
        " HESH",
        " Production Assistant"
    ],
    "profile_url": [
        "http://www.url1.com",
        "http://www.url2.com",
        "http://www.url3.com",
        "http://www.url4.com",
    ]
}]

I realized I made a mistake creating such list. json.loads() gives this error

Error

Expecting value: line 1 column 1 (char 0).

I want something like this.

[{
    "company": [
        "name": "THALIA HALL",
        "employee": {
            fullname: "emp_name",
            "position": "position",
            profile: "url"
        },
        {
            fullname: "emp_name",
            "position": "position",
            profile: "url"
        }
    ]
}]

How can I solve this problem? I need to do this on python.

Upvotes: 0

Views: 5522

Answers (2)

godot
godot

Reputation: 3545

import json

j  = '[{"company":["ABCD"],"fullname":["Bruce Lamont","Ariel Zilist","Bruce Lamont","Bobby Ramirez"],"position":[" The Hesh"," Server"," HESH"," Production Assistant"],"profile_url":["http://www.url1.com","http://www.url2.com","http://www.url3.com","http://www.url4.com"]}]'


json_obj = json.loads(j)

you have your JSON as object and now you can user for making csv

Upvotes: 0

vishal
vishal

Reputation: 1205

you are adding an extra comma in the end for profile_url array. The proper json should be

[{
    "company": [
        "ABCD"
    ],
    "fullname": [
        "Bruce Lamont",
        "Ariel Zilist",
        "Bruce Lamont",
        "Bobby Ramirez"
    ],
    "position": [
        " The Hesh",
        " Server",
        " HESH",
        " Production Assistant"
    ],
    "profile_url": [
        "http://www.url1.com",
        "http://www.url2.com",
        "http://www.url3.com",
        "http://www.url4.com"
    ]
}]

Use https://jsonformatter.curiousconcept.com/ to check for JSON formatting errors next time.

Upvotes: 1

Related Questions