![]() |
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 FormatBelow are some of the ways by which we can convert a bytes array into JSON format in Python:
Convert A Bytes Array Into Json Format Using
|
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'>
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.
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)) |
<class 'bytes'> {'key': 'value', 'nested': {'inner_key': [1, 2, 3]}} <class 'dict'>
decode
and io ModuleIn this example, we are using decode(), json.loads() and io module to convert a bytes array into JSON format in Python.
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'>
json.JSONDecoder
ClassIn this example, we create a custom JSON decoder using the json.JSONDecoder class, allowing us to handle complex scenarios or specific decoding requirements.
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)) |
<class 'bytes'> {'key': 'value', 'nested': {'inner_key': [1, 2, 3]}} <class 'dict'>
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |