How to Parse Json from Bytes in Python
Last Updated :
01 Mar, 2024
We are given a bytes object and we have to parse JSON object from it by using different approaches in Python. In this article, we will see how we can parse JSON with bytes in Python
Parse JSON With Bytes in Python
Below are some of the ways by which we can parse JSON with bytes in Python:
- Using the
json.loads()
function
- Decoding Bytes to Strings and Parsing JSON
- Handling Different Encodings
- Parsing JSON Data from an API Response
Using the json.loads()
function
In this example, we start with JSON data represented as bytes (json_data
). By decoding the bytes to a UTF-8 encoded string (decoded_data
), we can then use json.loads
to parse the JSON string into a Python dictionary (parsed_json
). Finally, the parsed JSON is printed to the console.
Python3
import json
json_data = b '{"name": "Amit", "age": 30, "city": "Delhi"}'
decoded_data = json_data.decode( 'utf-8' )
parsed_json = json.loads(decoded_data)
print (parsed_json)
|
Output
{'name': 'Amit', 'age': 30, 'city': 'Delhi'}
Decoding Bytes to Strings and Parsing JSON
In this example, we begin with JSON data represented as bytes (json_data
). By decoding the bytes to a UTF-8 encoded string (decoded_data
), we use json.loads
to parse the JSON string into a Python dictionary (parsed_json
). Subsequently, specific values such as name, age, and city are accessed from the parsed JSON, and these values are printed to the console.
Python3
import json
json_data = b '{"name": "Ankit", "age": 30, "city": "Mumbai"}'
decoded_data = json_data.decode( 'utf-8' )
parsed_json = json.loads(decoded_data)
name = parsed_json[ 'name' ]
age = parsed_json[ 'age' ]
city = parsed_json[ 'city' ]
print ( "Name:" , name)
print ( "Age:" , age)
print ( "City:" , city)
|
Output
Name: Ankit
Age: 30
City: Mumbai
Handling Different Encodings
In this example, we have JSON data encoded as bytes using UTF-16 (json_data_utf16
). By decoding the bytes with the appropriate encoding (utf-16
in this case) to obtain a string (decoded_data_utf16
), we then use json.loads
to parse the JSON string into a Python dictionary (parsed_json_utf16
). Subsequently, specific values such as name, age, and city are accessed from the parsed JSON with UTF-16 encoding, and these values are printed to the console.
Python3
import json
json_data_utf16 = b '\xff\xfe{\x00"\x00n\x00a\x00m\x00e\x00"\x00:\x00 \x00"\x00A\x00n\x00i\x00l\x00"\x00,\x00 \x00"\x00a\x00g\x00e\x00"\x00:\x00 \x002\x005\x00,\x00 \x00"\x00c\x00i\x00t\x00y\x00"\x00:\x00 \x00"\x00N\x00e\x00w\x00 \x00Y\x00o\x00r\x00k\x00"\x00}\x00'
decoded_data_utf16 = json_data_utf16.decode( 'utf-16' )
parsed_json_utf16 = json.loads(decoded_data_utf16)
name_utf16 = parsed_json_utf16[ 'name' ]
age_utf16 = parsed_json_utf16[ 'age' ]
city_utf16 = parsed_json_utf16[ 'city' ]
print ( "Name (UTF-16):" , name_utf16)
print ( "Age (UTF-16):" , age_utf16)
print ( "City (UTF-16):" , city_utf16)
|
Output
Name (UTF-16): Anil
Age (UTF-16): 25
City (UTF-16): New York
Parsing JSON Data from an API Response
In this example, a request is made to an API endpoint using urllib.request.urlopen
. The JSON data from the API response is obtained by reading the response (json_data_api
). By decoding the bytes to a UTF-8 encoded string (decoded_data_api
), json.loads
is used to parse the JSON string into a Python dictionary (parsed_json_api
). Subsequently, specific values such as user ID, title, and body are accessed from the parsed JSON, and these values are printed to the console.
Python3
import json
import urllib.request
response = urllib.request.urlopen(url)
json_data_api = response.read()
decoded_data_api = json_data_api.decode( 'utf-8' )
parsed_json_api = json.loads(decoded_data_api)
user_id = parsed_json_api[ 'userId' ]
title = parsed_json_api[ 'title' ]
body = parsed_json_api[ 'body' ]
print ( "User ID:" , user_id)
print ( "Title:" , title)
print ( "Body:" , body)
|
Output:
User ID: 1
Title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Body: quia et suscipit
suscipit recusandae consequuntur expedita et cum
Share your thoughts in the comments
Please Login to comment...