Open In App

Convert a Bytes Array into Json Format in Python

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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\', \
    \'Passwords\'],\'Link\':\'http://some_link.com\'}]'
 
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
 
# Sample complex bytes array
bytes_data = b'{"key": "value", "nested": {"inner_key": [1, 2, 3]}}'
print(type(bytes_data))
 
# Convert bytes to string and parse as JSON
json_data = json.loads(str(bytes_data, 'utf-8'))
 
# Display the resulting JSON data
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\',\
    \'Passwords\'],\'Link\':\'http://some_link.com\'}]'
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
 
# Sample complex bytes array
bytes_data = b'{"key": "value", "nested": {"inner_key": [1, 2, 3]}}'
 
# Custom JSON decoder for more complex scenarios
class ComplexDecoder(json.JSONDecoder):
    def decode(self, s, **kwargs):
        # Implement custom decoding logic if needed
        return super().decode(s, **kwargs)
 
print(type(bytes_data))
# Decode bytes using the custom decoder
json_data = json.loads(bytes_data, cls=ComplexDecoder)
 
# Display the resulting JSON data
print(json_data)
print(type(json_data))


Output

<class 'bytes'>
{'key': 'value', 'nested': {'inner_key': [1, 2, 3]}}
<class 'dict'>




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads