Delete Items from JSON-File - Python

Viewed 2705

I have a JSON file and would like to remove the beginning and end part

{
 "records":
    [{
         "id":"53c5d162-3dd4-41ea-afd1-378b5be1bf04",
         "userid":"u111124027",
         "username":"Testuser",
         "deviceid":"1185494411",
         "devicename":"",
         "start_date":"2020-05-19T06:46:55Z",
         "fee":0.0,
         "currency":"None",
         "billing_state":"Bill",
         "contact_id":"c410618636"
       }], 
"records_remaining":0
}

Parts to remove

{
"records": , 
"records_remaining":0
}

Code to remove the part

for element in data:
    if 'records' in element:
        del element['records']

Error:

del element['records']
TypeError: 'str' object does not support item deletion

If I remove the parts manually, I can manipulate the JSON via

if 'userid' in element:
    del element['userid']

Is there a possibility to remove the two parts? I address a rest API and save the -get into a JSON-File. To continue working with the JSON-file, I have to remove these two parts

At the end the json-file should look like this:

[{
    "id":"53c5d162-3dd4-41ea-afd1-378b5be1bf04",
    "userid":"u111124027",
    "username":"Testuser",
    "deviceid":"1185494411",
    "devicename":"",
    "start_date":"2020-05-19T06:46:55Z",
    "fee":0.0,
    "currency":"None",
    "billing_state":"Bill",
    "contact_id":"c410618636"
}]
2 Answers

It’s pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It’s done by using the json module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.

Deserialization of JSON

The Deserialization of JSON means the conversion of JSON objects into their respective Python objects. The load()/loads() method is used for it. If you have used JSON data from another program or obtained as a string format of JSON, then it can easily be deserialized with load()/loads(), which is usually used to load from string, otherwise the root object is in list or dict.

json.load(): json.load() accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.

Syntax:

json.loads(file object)

Normal reading from json would be -

# Python program to read 
# json file 


import json 

# Opening JSON file 
f = open('data.json',) 

# returns JSON object as  
# a dictionary 
data = json.load(f) 

# saving the records array
records = data["records"]

# Iterating through the json 
# list from records array
for i in data['records']: 
    print(i) 

# Closing file 
f.close() 

Now according to your use case, you just need the records and no other keys from json. rather than removing it from json, you can just access it with data["records"] that would solve the issue.

However, if you still want to remove it then you can use data["records"] from json and store it in a variable. You will then have to write that into the file for persistence.

Thanks a lot, I learned something new.

Now I can continue working with the file and convert it into a CSV.

with open('data.json', 'w') as f:
    json.dump(records, f)

df = pd.read_json('data.json')
df.to_csv('data.csv')


Related