Convert a Bytes Array into Json Format in Python
Last Updated :
06 Feb, 2024
In Python, dealing with bytes data is common, and converting a bytes array to JSON format is a frequent task. In this article, we will see how to convert a bytes array into JSON format in Python.
Python Convert a Bytes Array into JSON Format
Below are some of the ways by which we can convert a bytes array into JSON format in Python:
- Using
decode
and json.loads()
- Using
str()
with decode
and json.loads()
- Using
bytes.decode
and io Module
- Using
json.JSONDecoder
with Custom Decoder
Convert A Bytes Array Into Json Format Using decode
and json.loads
In this example, we decode the bytes array into a string using ‘utf-8’ encoding and then parse it as JSON using the json.loads method.
Python3
import json
byte = b '[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\' ,\
\ 'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\' , \
byte = byte.decode().replace( "'" , '"')
ans = json.loads(byte)
print (ans)
|
Output:
<class 'bytes'>
[{'Date': '2016-05-21T21:35:40Z',
'CreationDate': '2012-05-05',
'LogoType': 'png',
'Ref': 164611595,
'Classe': ['Email addresses', 'Passwords'],
'Link': 'http://some_link.com'}]
<class 'list'>
Bytes Array into JSON Format Using str()
with decode
and json.loads
In this example, we utilize the str constructor to convert the bytes array into a string and then parse it as JSON using json.loads.
Python3
import json
bytes_data = b '{"key": "value", "nested": {"inner_key": [1, 2, 3]}}'
print ( type (bytes_data))
json_data = json.loads( str (bytes_data, 'utf-8' ))
print (json_data)
print ( type (json_data))
|
Output
<class 'bytes'>
{'key': 'value', 'nested': {'inner_key': [1, 2, 3]}}
<class 'dict'>
Bytes Array into JSON Format Using decode
and io Module
In this example, we are using decode(), json.loads() and io module to convert a bytes array into JSON format in Python.
Python3
import json
import io
byte = b '[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\' , \
\ 'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\' ,\
print ( type (byte))
fix_bytes = byte.replace(b "'" , b'"')
my_json = json.load(io.BytesIO(fix_bytes))
print (my_json)
print ( type (my_json))
|
Output:
<class 'bytes'>
[{'Date': '2016-05-21T21:35:40Z',
'CreationDate': '2012-05-05',
'LogoType': 'png',
'Ref': 164611595,
'Classe': ['Email addresses', 'Passwords'],
'Link': 'http://some_link.com'}]
<class 'list'>
Convert Bytes Array into JSON Format Using json.JSONDecoder
Class
In this example, we create a custom JSON decoder using the json.JSONDecoder class, allowing us to handle complex scenarios or specific decoding requirements.
Python3
import json
bytes_data = b '{"key": "value", "nested": {"inner_key": [1, 2, 3]}}'
class ComplexDecoder(json.JSONDecoder):
def decode( self , s, * * kwargs):
return super ().decode(s, * * kwargs)
print ( type (bytes_data))
json_data = json.loads(bytes_data, cls = ComplexDecoder)
print (json_data)
print ( type (json_data))
|
Output
<class 'bytes'>
{'key': 'value', 'nested': {'inner_key': [1, 2, 3]}}
<class 'dict'>
Share your thoughts in the comments
Please Login to comment...