Convert nested list to JSON Python

I know that, list can be converted into JSON by using json.dumps[mylist].

But how can I convert something like this into JSON ?

[["abc", "bcd", "cde"] , ["pgr", "xyz"]]

diesch·3y

json.dumps can handle nested data structures, too.6Reply

sp3co92·3y

It worked. How can do the revert process.

I'm recieving a nested lists as a JSON value for a JSON key.This is the string I receive. "[['a','b','c','d'],['x','y','z']]"

I need to make this a list. [['a','b','c','d'],['x','y','z']] I tried convert it with this. But it gives me an error.  from io import StringIO io = StringIO[my_string] print[json.load[io]] Traceback [most recent call last]: File "/home/pankaja/PycharmProjects/bookdigitizer/testing/test1json.py", line 9, in print[json.load[io]] File "/home/pankaja/anaconda3/envs/research_project/lib/python3.6/json/__init__.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw] File "/home/pankaja/anaconda3/envs/research_project/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode[s] File "/home/pankaja/anaconda3/envs/research_project/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode[s, idx=_w[s, 0].end[]] File "/home/pankaja/anaconda3/envs/research_project/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError["Expecting value", s, err.value] from None json.decoder.JSONDecodeError: Expecting value: line 1 column 3 [char 2] 1Reply1 more reply

tunisia3507·3y

Doesn't JSON need strings to be double quoted?

tunisia3507·3y

What have you tried?1Reply

Beautah·3y

Are you trying to flatten it before you dump it?1Reply

ironhaven·3y

Think of a json as a python dictionary with a few less features. Luckily for you json can deal with arrays filled with arrays.

All you need is to put your array in a dictionary and Dumps will just return a valid json string

json.dumps[{key: myArray}]1Reply

Video liên quan

Chủ Đề