Your bytes object is almost JSON, but it's using single quotes instead of double quotes, and it needs to be a string. So one way to fix it is to decode the bytes to str and replace the quotes. Another option is to use ast.literal_eval; see below for details. If you want to print the result or save it to a file as valid JSON you can load the JSON to a Python list and then dump it out. Eg,

import json

my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'

# Decode UTF-8 bytes to Unicode, and convert single quotes 
# to double quotes to make it valid JSON
my_json = my_bytes_value.decode('utf8').replace("'", '"')
print(my_json)
print('- ' * 20)

# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
s = json.dumps(data, indent=4, sort_keys=True)
print(s)

output

[{"Date": "2016-05-21T21:35:40Z", "CreationDate": "2012-05-05", "LogoType": "png", "Ref": 164611595, "Classe": ["Email addresses", "Passwords"],"Link":"http://some_link.com"}]
- - - - - - - - - - - - - - - - - - - - 
[
    {
        "Classe": [
            "Email addresses",
            "Passwords"
        ],
        "CreationDate": "2012-05-05",
        "Date": "2016-05-21T21:35:40Z",
        "Link": "http://some_link.com",
        "LogoType": "png",
        "Ref": 164611595
    }
]

As Antti Haapala mentions in the comments, we can use ast.literal_eval to convert my_bytes_value to a Python list, once we've decoded it to a string.

from ast import literal_eval
import json

my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'

data = literal_eval(my_bytes_value.decode('utf8'))
print(data)
print('- ' * 20)

s = json.dumps(data, indent=4, sort_keys=True)
print(s)

Generally, this problem arises because someone has saved data by printing its Python repr instead of using the json module to create proper JSON data. If it's possible, it's better to fix that problem so that proper JSON data is created in the first place.

Answer from PM 2Ring on Stack Overflow
Top answer
1 of 9
216

Your bytes object is almost JSON, but it's using single quotes instead of double quotes, and it needs to be a string. So one way to fix it is to decode the bytes to str and replace the quotes. Another option is to use ast.literal_eval; see below for details. If you want to print the result or save it to a file as valid JSON you can load the JSON to a Python list and then dump it out. Eg,

import json

my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'

# Decode UTF-8 bytes to Unicode, and convert single quotes 
# to double quotes to make it valid JSON
my_json = my_bytes_value.decode('utf8').replace("'", '"')
print(my_json)
print('- ' * 20)

# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
s = json.dumps(data, indent=4, sort_keys=True)
print(s)

output

[{"Date": "2016-05-21T21:35:40Z", "CreationDate": "2012-05-05", "LogoType": "png", "Ref": 164611595, "Classe": ["Email addresses", "Passwords"],"Link":"http://some_link.com"}]
- - - - - - - - - - - - - - - - - - - - 
[
    {
        "Classe": [
            "Email addresses",
            "Passwords"
        ],
        "CreationDate": "2012-05-05",
        "Date": "2016-05-21T21:35:40Z",
        "Link": "http://some_link.com",
        "LogoType": "png",
        "Ref": 164611595
    }
]

As Antti Haapala mentions in the comments, we can use ast.literal_eval to convert my_bytes_value to a Python list, once we've decoded it to a string.

from ast import literal_eval
import json

my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'

data = literal_eval(my_bytes_value.decode('utf8'))
print(data)
print('- ' * 20)

s = json.dumps(data, indent=4, sort_keys=True)
print(s)

Generally, this problem arises because someone has saved data by printing its Python repr instead of using the json module to create proper JSON data. If it's possible, it's better to fix that problem so that proper JSON data is created in the first place.

2 of 9
119

You can simply use,

import json

my_bytes_value = my_bytes_value.decode().replace("'", '"')
json.loads(my_bytes_value)
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-python-bytes-to-json
5 Best Ways to Convert Python Bytes to JSON – Be on the Right Side of Change
February 23, 2024 - It’s generally used for strings that only contain Python literals, but care should be taken since this method is not meant for processing JSON. ... import ast bytes_data = b'{"name":"Alice","age":30}' string_data = bytes_data.decode('utf-8') json_data = ast.literal_eval(string_data) print(json_data)
Discussions

python - How to encode bytes in JSON? json.dumps() throwing a TypeError - Stack Overflow
I am trying to encode a dictionary containing a string of bytes with json, and getting a is not JSON serializable error: import base64 import json data = {} encoded = base64.b64encode(b'data to be More on stackoverflow.com
🌐 stackoverflow.com
Python decoding | Bytes to json - Stack Overflow
Ogun Jimmy over Politiek - constructief ... json.loads(payload_decode,strict=False) Thank you for your help! ... Can you provide the error you're getting? It's a little unclear what you're trying to do. That said, json.dumps() will convert something TO json, json.loads() will read something FROM json. Your payload does not have to be a bytestring or a string at all. You can have your data be a python dict, and ... More on stackoverflow.com
🌐 stackoverflow.com
json - python byte string encode and decode - Stack Overflow
Remember: decode goes from bytes to unicode. encode goes from unicode to bytes. ... Sure, I wasn't arguing, just providing some extra explanation for the OP. ... I had come up with the same thing but it seemed so inefficient. I am surprised there isn't a way to directly utf-8 encode a byte string. More on stackoverflow.com
🌐 stackoverflow.com
March 7, 2012
Python 3 bytes, string JSON problems
It seems that when running python 3.5 · the encode_data method does not always work as expected as it return bytes and json expects string objects. For example if the executor does not speciy a uuid the default assignment lead to JSON serialization errors. Adding a encode_data(uuid.uuid4().bytes).decode... More on github.com
🌐 github.com
4
November 8, 2016
🌐
TutorialsPoint
tutorialspoint.com › article › How-can-I-convert-a-bytes-array-into-JSON-format-in-Python
Convert Bytes Array to JSON Format in Python
March 5, 2020 - This can be done using the decode function from string class that will accept then encoding you want to decode with. 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) ...
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
February 23, 2026 - The RFC permits, but does not require, JSON deserializers to ignore an initial BOM in their input. This module’s deserializer raises a ValueError when an initial BOM is present. The RFC does not explicitly forbid JSON strings which contain byte sequences that don’t correspond to valid Unicode characters (e.g.
🌐
Like Geeks
likegeeks.com › home › python › how to convert bytes array to json in python
How To Convert Bytes Array to JSON in Python
January 24, 2024 - import json from codecs import BOM_UTF8 bytes_data = BOM_UTF8 + b'{"feedback": "Excellent", "usage": 23.7}' clean_data = bytes_data.lstrip(BOM_UTF8) json_data = json.loads(clean_data.decode('utf-8')) print(json_data) ... This output shows a successful conversion to a Python dictionary, free from any issues caused by the BOM. Sometimes, you might encounter a byte array that contains an escaped JSON string.
Top answer
1 of 1
3

The main problem is that without the r as a string prefix, the backslashes (\) disappear from the string. So the problem is not an encoding question but only the problem of providing a valid json string, be it in bytes or unicode characters. Just print the string after the payload = ... statement to see that the string is broken and cannot be parsed because it contains:

  • raw new line characters inside strings
  • raw quote characters (") inside strings

Once this is fixed:

payload = rb'{"content":[{"text":{"type":"text","format":"plain","text":"Luc Nilis heeft een nieuwe club gevonden en volgt Belgische coach naar Turkije\n\nHurlu Dedju over Beerschot bekijkt mogelijkheden om aandeel Saoedische prins te vergroten Bandencentrale Luyckx over Zware tegenvaller(s) bij AA Gent: recordaankoop mogelijk lang out, eerste seizoenshelft zit er waarschijnlijk op voor andere versterking Pxc3xa4rs over De kwelduivel van het Anderlechtse middenveld: \"Ik moest Vlap uit de match houden. Het liep wel goed\" Warlord over Meerderheid van Beerschot-aandelen straks in buitenlandse handen? \"We zijn de aandelenstructuur aan het herbekijken\" BlueWhiteSky over AA Gent, vijf maanden geen wedstrijd en dan zo slap spelen: \"We misten goesting, agressiviteit en concentratie\" Hurlu Dedju over Nieuw Genks koningskoppel in de maak? Cyriel Dessers en Paul Onuachu hebben deze reactie in huis na Genkse zege Hurlu Dedju over Vercauteren geeft Vrancken gelijk: \"Wij hebben ook twee punten weggegeven, maar met al de rest ga ik akkoord\" SnellenEddy over POLL: (Mogelijk) geen Belgisch voetbal op Telenet: wat met uw abo? Ogun Jimmy over Politiek - constructief en respectvol discussie-forum Blauw zwart Jan Breydel over Ver het enige lichtpunt bij Anderlecht, de terugkeer van Dimata: \"Maar dit mag niet gebeuren!\" Kantine"}}]}'

You can load either from the byte string:

json.loads(payload)

or from an unicode string:

json.loads(payload.decode())
🌐
Python
bugs.python.org › issue10976
Issue 10976: accept bytes in json.loads() - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/55185
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-bytes-array-into-json-format-in-python
Convert a Bytes Array into Json Format in Python - GeeksforGeeks
July 23, 2025 - import json # Sample complex bytes ... as JSON json_data = json.loads(str(bytes_data, 'utf-8')) # Display the resulting JSON data print(json_data) print(type(json_data)) ... In this example, we are using decode(), json.loads() and io module to convert ...
🌐
The Geek Stuff
thegeekstuff.com › 2021 › 03 › python-json-encode-decode
5 Python Examples to Read and Write JSON files for Encode and Decode
March 31, 2021 - Refer to this for additional information: 9 Python if, if else, if elif Command Examples · Json module allows the user to parse json in the form of strings, bytes or byte array into dictionary. This is done using function json.loads() . Note the similarity in the function name with the previous ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-parse-json-from-bytes-in-python
How to Parse Json from Bytes in Python - GeeksforGeeks
March 1, 2024 - 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).
🌐
GeeksforGeeks
geeksforgeeks.org › convert-bytes-to-json-using-python
Convert Bytes To Json using Python - GeeksforGeeks
February 7, 2024 - Below are some of the ways by which we can convert bytes to JSON in Python: ... In this example, we use the json.loads() method and decode() method to convert bytes to JSON objects.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-python-bytes-to-json-string
5 Best Ways to Convert Python Bytes to JSON String – Be on the Right Side of Change
February 23, 2024 - This method involves decoding the bytes object into a string using the decode() method, then parsing it with json.loads() to turn it into a Python dictionary, which is finally converted into a JSON string with json.dumps().
🌐
GitHub
github.com › douban › pymesos › issues › 33
Python 3 bytes, string JSON problems · Issue #33 · douban/pymesos
November 8, 2016 - It seems that when running python 3.5 · the encode_data method does not always work as expected as it return bytes and json expects string objects. For example if the executor does not speciy a uuid the default assignment lead to JSON serialization errors. Adding a encode_data(uuid.uuid4().bytes).decode(), solves this.
Author   Arttii
🌐
Python Forum
python-forum.io › thread-31323.html
bytes to json
Hi I am trying to send json data over a pipe but cannot get it quite right Send mesages = [{'id': '1', 'txt': 'message 1', 'start_date': '2014-02-09', 'start_time': '16:20', 'duration': '01:00'}] win32file.WriteFile(handle, str.encode(jso
Top answer
1 of 3
4

The most important part to properly answer this is the information on how you pass these objetcts to the Python2 program: you are using JSON.

So, stay with me:

After you do the .encode step in program 1, you have a bytes object. By calling str(...) on it, you are just putting a escaping layer on this bytes object, and turning it back to a string - but when this string is written as is to a file, or transmited over the network, it will be encoded again - any non-ASCII tokens are usually escaped with the \u prefix and the codepoint for each character - but the original Chinese chracters themselves are now encoded in utf-8 and doubly-escaped.

Python's JSON load methods already decode the contents of json data into text-strings: so a decode method is not to be expected at all.

In short: to pass data around, simply encode your original text as JSON in the first program, and do not botter with any decoding after json.load on the target Python 2 program:

# my first program
x = "宇宙"
# No str-encode-decode dance needed here.
...
data =  json.dumps({"example_key": x, ...})
# code to transmit json string by network or file as it is...


# my other program
text = json.loads(data)["example_key"]
# text is a Unicode text string ready to be used!

As you are doing, you are probably gettint the text doubly-encoded - I will mimick it on the Python 3 console. I will print the result from each step so you can undestand the transforms that are taking place.

In [1]: import json

In [2]: x = "宇宙"

In [3]: print(x.encode("utf-8"))
b'\xe5\xae\x87\xe5\xae\x99'

In [4]: text = str(x.encode("utf-8"))

In [5]: print(text)
b'\xe5\xae\x87\xe5\xae\x99'

In [6]: json_data = json.dumps(text)

In [7]: print(json_data)
"b'\\xe5\\xae\\x87\\xe5\\xae\\x99'"
# as you can see, it is doubly escaped, and it is mostly useless in this form

In [8]: recovered_from_json = json.loads(json_data)

In [9]: print(recovered_from_json)
b'\xe5\xae\x87\xe5\xae\x99'

In [10]: print(repr(recovered_from_json))
"b'\\xe5\\xae\\x87\\xe5\\xae\\x99'"

In [11]: # and if you have data like this in files/databases you need to recover:

In [12]: import ast

In [13]: recovered_text = ast.literal_eval(recovered_from_json).decode("utf-8")

In [14]: print(recovered_text)
宇宙
2 of 3
0

I had this issue when decoding stringified bytes received in a JSON payload:

print(value)
>>> "b'text-here'"

Encoding the string only compounded the problem by adding another layer of wrapping:

encoded = value.encode("utf-8")
print(encoded)
>>> b"b'text-here'"

And this solution from @jsbueno didn't work for me - I got a json.decoder.JSONDecodeError:

recovered_from_json = json.loads(json_data)

SOLUTION

You need to evaluate the string to expose the wrapped bytes object:

import ast

as_bytes = ast.literal_eval(value)
print(as_bytes)
>>> b'text-here'

Then you can decode to string:

decoded = value.decode()
print(decoded)
>>> 'text-here'

As noted by @snakecharmerb, you shouldn't use eval() as it opens you up to running potentially dangerous commands if input is not checked or sanitized (see this old but illustrative blog post)

🌐
Readthedocs
simplejson.readthedocs.io
simplejson — JSON encoder and decoder — simplejson 3.19.1 documentation
For backwards compatibility with versions of simplejson earlier than 2.1.0, an integer is also accepted and is converted to a string with that many spaces. If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace. If encoding is not None, then all input bytes objects in Python 3 and 8-bit strings in Python 2 will be transformed into unicode using that encoding prior to JSON-encoding.
🌐
LAVA
lavag.org › software & hardware discussions › labview general
Convert to JSON string a string from "Byte Array to String" - LabVIEW General - LAVA
March 9, 2024 - Dear all, I have to transmit a JSON message with a string of chars to an external REST API (in python or javescript). In LabVIEW I have an array of uint8, converted to a string with "Byte Array to String.vi". This string has the expected length (equal to the length of the uint8 array). When I con...