How To Parse A Json Object Into Smaller Objects Using Python?
I have a very large JSON object that I need to split into smaller objects and write those smaller objects to file. Sample Data raw = '[{'id':'1','num':'2182','count':-17}{'id':'111
Solution 1:
assuming the raw should be a valid json string (I included the missing commas), here is a simple, but working solution.
import json
raw = '[{"id":"1","num":"2182","count":-17},{"id":"111","num":"3182","count":-202},{"id":"222","num":"4182","count":12},{"id":"33333","num":"5182","count":12}]'
json_data = json.loads(raw)
def split_in_files(json_data, amount):
step = len(json_data) // amount
pos = 0foriinrange(amount - 1):
with open('output_file{}.json'.format(i+1), 'w') as file:
json.dump(json_data[pos:pos+step], file)
pos += step
# last one
with open('output_file{}.json'.format(amount), 'w') as file:
json.dump(json_data[pos:], file)
split_in_files(json_data, 2)
Solution 2:
if raw is valid json. the saving part is not detailed.
import json
raw = '[{"id":"1","num":"2182","count":-17},{"id":"111","num":"3182","count":-202},{"id":"222","num":"4182","count":12},{"id":"33333","num":"5182","count":12}]'
raw_list = eval(raw)
raw__zipped = list(zip(raw_list[0::2], raw_list[1::2]))
foritemin raw__zipped:
with open('a.json', 'w') as f:
json.dump(item, f)
Solution 3:
If you need the exactly half of the data you can use slicing:
import json
raw = '[{"id":"1","num":"2182","count":-17},{"id":"111","num":"3182","count":-202},{"id":"222","num":"4182","count":12},{"id":"33333","num":"5182","count":12}]'
json_data = json.loads(raw)
size_of_half = len(json_data)/2
print json_data[:size_of_half]
print json_data[size_of_half:]
In shared code basic cases are not handled like what if length is odd etc, In short You can do everything that you can do with list.
Post a Comment for "How To Parse A Json Object Into Smaller Objects Using Python?"