Iam trying to remove some key,value from my json output, but none of the things seems to be working.
The code solution is basically to convert a csv to json and then removed the unwanted keys from it.
import csv, json
import pandas as pd
import os
def make_json(csvFilePath, jsonFilePath):
data = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
key = rows['ipadd']
data[key] = rows
#used del but not working as expected
#del data['number']
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
csvFilePath = r'csvfile.csv'
jsonFilePath = r'demo.json'
make_json(csvFilePath, jsonFilePath)
I tried with del data['key_name']
. Thought of manipulate csv before json creation, but then the key wouldnt be available for json creation.
{
"some_random_key": {
"number": "12345",
"ipadd": "some_random_key",
"version": "0",
"name": "superman",
"devicename": "0",
"osversion": "0"
},
"some_random_key2": {
"number": "54321",
"ipadd": "some_random_key2",
"version": "0",
"name": "batman",
"devicename": "0",
"osversion": "0"
}
}
Expected outcome:
{
"some_random_key": {
"version": "0",
"name": "superman",
"devicename": "0",
"osversion": "0"
},
"some_random_key2": {
"version": "0",
"name": "batman",
"devicename": "0",
"osversion": "0"
}
}
You are looking for dict.pop()
, which does exactly what you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.