Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array of JSON String to array of object in Python

I having array of JSON string, I need to convert it as array of object (i.e., convert the JSON to respective object) without a for loop.

Source Code: (Input data)

data = ['[1,2,3]', '[4,5,6]', '[7,8,9]']

Required Output:

[[1,2,3], [4,5,6], [7,8,9]]

I already using the following solution

import json

data = ['[1,2,3]', '[4,5,6]', '[7,8,9]']
output = []
for item in data:
    output.append(json.loads(item))

Currently I'm having a very large number of JSON strings (approx 100K records) and moreover each JSON String array internally contains the record approx 50K. While on execution it takes more than 3GB of RAM for processing.

Note: Implicitly the output is a 2-dim array [][]. 1st dimension is approx 100K records 2nd dimension contains approx 50K records. Totally 100K * 50K items.

While on conversion it takes more time to convert the JSON (for the above approach). Kindly assist me the idea to convert the JSON string without a for loop.

like image 514
Sreedharan Avatar asked Feb 23 '26 09:02

Sreedharan


1 Answers

Now solution looks wired but this works and will be useful for you in optimization. Convert the complete list into str then remove all ' single commas with str function and the apply json loads, hurray this has got worked for me.

data = ['[1,2,3]', '[4,5,6]', '[7,8,9]']
r = str(data).replace("'",'')

import json
data = json.loads(r)

now your data will be of list of list without looping. You can achieve this.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
like image 130
Anup Yadav Avatar answered Feb 25 '26 21:02

Anup Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!