How to Parse Json from Bytes in Python
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
- 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 # Example JSON data with bytes json_data = b '{"name": "Amit", "age": 30, "city": "Delhi"}' # Decode bytes to string and parse JSON decoded_data = json_data.decode( 'utf-8' ) parsed_json = json.loads(decoded_data) # Print parsed JSON print (parsed_json) |
{'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 # Example JSON data with bytes json_data = b '{"name": "Ankit", "age": 30, "city": "Mumbai"}' # Decode bytes to string and parse JSON decoded_data = json_data.decode( 'utf-8' ) parsed_json = json.loads(decoded_data) # Accessing values from parsed JSON name = parsed_json[ 'name' ] age = parsed_json[ 'age' ] city = parsed_json[ 'city' ] # Printing parsed values print ( "Name:" , name) print ( "Age:" , age) print ( "City:" , city) |
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 # Example JSON data with bytes encoded using different encodings 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' # Decode bytes with different encodings decoded_data_utf16 = json_data_utf16.decode( 'utf-16' ) parsed_json_utf16 = json.loads(decoded_data_utf16) # Accessing values from parsed JSON name_utf16 = parsed_json_utf16[ 'name' ] age_utf16 = parsed_json_utf16[ 'age' ] city_utf16 = parsed_json_utf16[ 'city' ] # Printing parsed values print ( "Name (UTF-16):" , name_utf16) print ( "Age (UTF-16):" , age_utf16) print ( "City (UTF-16):" , city_utf16) |
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 # Make a request to an API endpoint url = 'https://jsonplaceholder.typicode.com/posts/1' response = urllib.request.urlopen(url) # Get the JSON data from the response json_data_api = response.read() # Decode bytes to string and parse JSON decoded_data_api = json_data_api.decode( 'utf-8' ) parsed_json_api = json.loads(decoded_data_api) # Accessing values from parsed JSON user_id = parsed_json_api[ 'userId' ] title = parsed_json_api[ 'title' ] body = parsed_json_api[ 'body' ] # Printing parsed values 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
Contact Us