Home > Blockchain >  How to convert binary data to json
How to convert binary data to json

Time:10-15

I want to convert the below data to json in python.

I have the data in the following format.

b'{"id": "1", "name": " value1"}\n{"id":"2", name": "value2"}\n{"id":"3", "name": "value3"}\n'

This has multiple json objects separated by \n. I was trying to load this as json .

converted the data into string first and loads as json but getting the exception.

my_json = content.decode('utf8')
json_data = json.loads(my_json)
   raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 2306)

CodePudding user response:

You need to decode it then split by '\n' and load each json object separately. If you store your byte string in a variable called byte_string you could do something like:

json_str = byte_string.decode('utf-8')
json_objs = json_str.split('\n')
for obj in json_objs:
    json.loads(obj)

For the particular string that you have posted here though, you will get an error on the second object because the second key in it is missing a double quote. It is name" in the string you linked.

CodePudding user response:

What have to first decode your json to a string. So you can just say:

your_json_string = the_json.decode()

now you have a string.

Now what you want to do is:

your_json_string = your_json_string.replace("\\n", "") 

so you are replacing the \n with nothing basically. Note that the two backslashes are required, this is not a typo.

Now you can just say:

your_json = json.loads(your_json_string)

CodePudding user response:

First, this isn't valid json since it's not a single object. Second, there is a typo: the "id":"2" entry is missing a double-quote on the name property element.

An alternative to processing one dict at a time, you can replace the newlines with "," and turn it into an array. This is a fragile solution since it requires exactly one newline between each dict, but is compact:

s = b'{"id": "1", "name": " value1"}\n{"id":"2", "name": "value2"}\n{"id":"3", "name": "value3"}\n'

my_json = s.decode('utf8')
json_data = json.loads("["   my_json.rstrip().replace("\n", ",")   "]")
  • Related