-1

Guys where I get multiple data each in a list from a database and append this to another list,so my data looks this way

data= [["a","b","c"],["1","2","3"]]

If I try to Json dump and load this data it give an error on the load side

data2 = str.encode(json.dumps(data))
st = bytes.decode(data2)
msg = json.loads(st)

raise JSONDecodeError("Expecting value", s,err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

But this doesn't not happen where it is a single list

PyCobra
  • 7
  • 4
  • 2
    what's with all the str/bytes encoding/decoding? why not just `json.dumps(data)` and `json.loads` for the other way around? – DeepSpace Jan 30 '20 at 09:51
  • Have you checked [https://stackoverflow.com/questions/42710879/write-two-dimensional-list-to-json-file](https://stackoverflow.com/questions/42710879/write-two-dimensional-list-to-json-file) ? – lsabi Jan 30 '20 at 09:51
  • The code you have shown doesn't reproduce the problem. – MisterMiyagi Jan 30 '20 at 10:00
  • This should answer the question pretty well - – Prashant Kumar Jan 30 '20 at 10:05
  • Does this answer your question? [Serializing list to JSON](https://stackoverflow.com/questions/2147701/serializing-list-to-json) – Prashant Kumar Jan 30 '20 at 10:08
  • My script isn't as straightforward as this,I wrote a simple code so my question ll be more efficient ,my script involves a threaded server and a client side,I ll probably edit it so it reflects the exact nature of my code – PyCobra Jan 30 '20 at 16:49

3 Answers3

1

I'm not sure what you tried to achieve with all the str/bytes encoding/decoding, but this is as straightforward as

import json

data = [["a", "b", "c"], ["1", "2", "3"]]
json_str = json.dumps(data)
print(json_str)
# '[["a", "b", "c"], ["1", "2", "3"]]' <- a string
python_list = json.loads(json_str)
print(python_list)
# [['a', 'b', 'c'], ['1', '2', '3']] <- back to Python list
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • My script isn't as straightforward as this,I wrote a simple code so my question ll be more efficient ,my script involves a threaded server and a client side,I ll probably edit it so it reflects the exact nature of my code – PyCobra Jan 30 '20 at 16:47
1

Here is a Solution for Json Dump and Load this data.

import json
data= [["a","b","c"],["1","2","3"]]

# print(data)

data2 = json.dumps(data)
print(data2)
msg = json.loads(data2)
print(msg)
Nikhil B
  • 93
  • 6
-1

do you want json like this : {"data": [["a", "b", "c"], ["1", "2", "3"]]}

for this output this is the code

data= [["a","b","c"],["1","2","3"]]
import json
data_dict = {'data':data}
data2 = json.dumps(data_dict)
print(data2)
  • Thank you all guys I found a way around it,though it's not the best,turns out Json.load has issues with a list that contains too many list with too many items in each,so after dumping, it cannot load it,I would like to know why json behaves like this,this has made my job more difficult,cos now where I have an sql TABLE with 100columns.instead of using a sql statement (SELECT * FROM TABLE),in other to avoid json error I have to get it in smaller chunks (SELECT col1,col2,col3 FROM TABLE) – PyCobra Feb 28 '20 at 06:15