- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I convert a bytes array into JSON format in Python?
You need to decode the bytes object to produce a string. This can be done using the decode function from string class that will accept then encoding you want to decode with.
example
my_str = b"Hello" # b means its a byte string new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding print(new_str)
Output
This will give the output
Hello
Once you have the bytes as a string, you can use the JSON.dumps method to convert the string object to JSON.
example
my_str = b'{"foo": 42}' # b means its a byte string new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding import json d = json.dumps(my_str) print(d)
Output
This will give the output −
"{\"foo\": 42}"
- Related Articles
- How can I convert bytes to a Python string?
- How I can dump a Python tuple in JSON format?
- How can I represent python tuple in JSON format?
- How we can convert Python objects into JSON objects?
- How to convert Python date in JSON format?
- Convert JSON array into normal json in JavaScript
- How to print Python dictionary into JSON format?
- How I can convert a Python Tuple into Dictionary?
- How can I convert Python strings into tuple?
- How to convert JSON data into a Python tuple?
- How to convert JSON data into a Python object?
- How can I convert a Python tuple to an Array?
- How can I convert Python tuple to C array?
- How can we convert a list to the JSON array in Java?
- Convert JSON to another JSON format with recursion JavaScript

Advertisements