Delete info from a JSON using Python -
i exporting database firebase json , want upload bigquery. however, of fieldnames in database have nested information , bigquery not accept way. how can delete 'peripherals' every dataset present in in json. not present in every dataset though. provided example of json code looks below. help!
{"appname": "dataworks", "foundedperipheralcount": 1, "version": "1.6.1(8056)", "devicetype": "iphone 6", "createdat": "2017-04-05t07:05:30.408z", "updatedat": "2017-04-05t07:08:49.569z", "peripherals": {"1ca726ed-32b1-43b4-9071-b58bbace20a8": "arduino"}, "connectedperipheralcount": 1, "iosversion": "10.2.1"} {"objectid": "20h5hg2inb", "foundedperipheralcount": 0, "devicevendorid": "5b7f085e-b3b6-4270-97dc-f42903cdeac1", "version": "1.3.5(5801)", "devicetype": "iphone 6", "createdat": "2015-11-10t06:16:45.459z", "updatedat": "2015-11-10t06:16:45.459z", "connectedperipheralcount": 0, "iosversion": "9.1"} {"appname": "dataworks", "foundedperipheralcount": 2, "version": "1.6.2(8069)", "devicetype": "iphone 6s", "createdat": "2017-04-12t10:05:05.937z", "updatedat": "2017-07-06t07:33:02.006z", "peripherals": {"060ebafd-3120-4aad-8b0a-ec14a323fa25": "28902 ", "identifierinternalsensors": "internal sensors", "0521a273-faa5-462e-b9ec-fbb3d60f5e99": "28895 "}, "connectedperipheralcount": 8, "iosversion": "10.2.1"}
i have tried
import json open('firetobq_peripheral.json') out_file: out = json.load(out_file) element in out: del element['peripherals'] print(out)
but receive error
traceback (most recent call last): file "editjson.py", line 3, in <module> out = json.load(out_file) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/__init__.py", line 290, in load **kw) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads return _default_decoder.decode(s) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode raise valueerror(errmsg("extra data", s, end, len(s))) valueerror: data: line 2 column 1 - line 629 column 1 (char 311 - 203056)
it looks data in 'firetobq_peripheral.json'
not valid json. if each row on new line can use code:
with open('firetobq_peripheral.json', 'r') in_file: dicts = [] line in in_file.readlines() : d = json.loads(line.strip()) if d.get('peripherals'): del d['peripherals'] dicts += [d] open('firetobq_peripheral.json', 'w') out_file: out_file.write('[\n') i,v in enumerate(dicts): out_file.write(json.dumps(v)+('\n' if == len(dicts)-1 else ',\n')) out_file.write(']')
use code formatted json data:
with open('firetobq_peripheral.json', 'r') in_file: dicts = json.load(in_file) d in dicts: if d.get('peripherals'): del d['peripherals'] open('firetobq_peripheral.json', 'w') out_file: out_file.write(json.dumps(dicts, indent=2))
Comments
Post a Comment