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
๐ŸŒ
Javainuse
javainuse.com โ€บ bytesizejson
Online JSON Size Calculator Tool (In Bytes)
Hex Encoder/Decoder Convert text ... to GeoJSON geographic format JSON to GraphQL Generate GraphQL schema from JSON data Java Decompiler View Java classes on the fly JSON -> Java POJO Generate getters/setters from JSON Text Size (Bytes) Measure string length with/without spaces ...
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)
๐ŸŒ
myCompiler
mycompiler.io โ€บ view โ€บ 1i7xQl17MrP
bytes to json converter (TypeScript) - myCompiler
December 26, 2023 - // Modified function interface BufferObject { type: string; data: number[]; } function ConvertBytesToJson(jsonData: string, callback?: (key: string, value: string) => void): { key: string; value: string } { // Default callback to store key-value pair const defaultCallback: (key: string, value: string) => void = (key, value) => { keyValueObject.key = key; keyValueObject.value = value; console.log(`Key: ${key}`); }; // Use the provided callback or the default one const actualCallback = callback || defaultCallback; // Object to store key-value pair const keyValueObject: { key: string; value: stri
๐ŸŒ
Cloudmersive
cloudmersive.com โ€บ convert-file-to-json-byte-array-tool
Convert File to JSON Byte Array Tool - Cloudmersive APIs
Convert files and content between file formats. Instantly ยท Instantly convert an uneditable PDF back into an editable Word Document at very high fidelity
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-bytes-to-json-using-python
Convert Bytes To Json using Python - GeeksforGeeks
July 23, 2025 - In this example, we use the json.loads() method and decode() method to convert bytes to JSON objects.
Top answer
1 of 6
94

Here is a good example of base64 encoding byte arrays. It gets more complicated when you throw unicode characters in the mix to send things like PDF documents. After encoding a byte array the encoded string can be used as a JSON property value.

Apache commons offers good utilities:

 byte[] bytes = getByteArr();
 String base64String = Base64.encodeBase64String(bytes);
 byte[] backToBytes = Base64.decodeBase64(base64String);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding

Java server side example:

public String getUnsecureContentBase64(String url)
        throws ClientProtocolException, IOException {

            //getUnsecureContent will generate some byte[]
    byte[] result = getUnsecureContent(url);

            // use apache org.apache.commons.codec.binary.Base64
            // if you're sending back as a http request result you may have to
            // org.apache.commons.httpclient.util.URIUtil.encodeQuery
    return Base64.encodeBase64String(result);
}

JavaScript decode:

//decode URL encoding if encoded before returning result
var uriEncodedString = decodeURIComponent(response);

var byteArr = base64DecToArr(uriEncodedString);

//from mozilla
function b64ToUint6 (nChr) {

  return nChr > 64 && nChr < 91 ?
      nChr - 65
    : nChr > 96 && nChr < 123 ?
      nChr - 71
    : nChr > 47 && nChr < 58 ?
      nChr + 4
    : nChr === 43 ?
      62
    : nChr === 47 ?
      63
    :
      0;

}

function base64DecToArr (sBase64, nBlocksSize) {

  var
    sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
    nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);

  for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
    nMod4 = nInIdx & 3;
    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
    if (nMod4 === 3 || nInLen - nInIdx === 1) {
      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
        taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
      }
      nUint24 = 0;

    }
  }

  return taBytes;
}
2 of 6
15

The typical way to send binary in json is to base64 encode it.

Java provides different ways to Base64 encode and decode a byte[]. One of these is DatatypeConverter.

Very simply

byte[] originalBytes = new byte[] { 1, 2, 3, 4, 5};
String base64Encoded = DatatypeConverter.printBase64Binary(originalBytes);
byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);

You'll have to make this conversion depending on the json parser/generator library you use.

๐ŸŒ
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 - In this example, we are using decode(), json.loads() and io module to convert a bytes array into JSON format in Python.
Find elsewhere
๐ŸŒ
Online Tools
onlinetools.com โ€บ json โ€บ convert-base64-to-json
Convert Base64 to JSON โ€“ Online JSON Tools
Free online base64 to JSON converter. Just load your base64 in the input field and it will automatically get converted to JSON. It can't get any easier than this!
๐ŸŒ
Quora
quora.com โ€บ How-do-you-pass-a-byte-array-in-a-JSON-request
How to pass a byte array in a JSON request - Quora
You'll need to encode your byte array in a character format. Base64 encoding is a popular choice. ... Passing a byte array in JSON requires encoding the binary data into a text-safe representation and specifying how the recipient should decode it.
๐ŸŒ
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 - Learn to convert byte arrays to JSON in Python, handling various formats like UTF-8, BOM, and more, with this easy-to-follow tutorial.
๐ŸŒ
Medium
priyarajtt.medium.com โ€บ java-program-to-convert-byte-array-to-json-38548b53e27f
Java Program to Convert Byte Array to JSON | by priya raj | Medium
July 12, 2022 - We can beautify and see the output as follows. Output is in the format of JSON Array ... The main important thing in converting byte array to JSON is that the byte array should be of the pattern to get parsed using JsonParser.
๐ŸŒ
openHAB
openhab.org โ€บ addons โ€บ transformations โ€บ bin2json
Binary To JSON - Transformation Services | openHAB
Binary data contains 3 bytes and strict data format is following byte a; byte b; ubyte c;. Binary to JSON converter will return following result {"a":3,"b":-6,"c":255}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-convert-byte-array-to-json
JavaScript Program to Convert Byte Array to JSON - GeeksforGeeks
July 23, 2025 - Converting a byte array to JSON means transforming a sequence of bytes into a structured JSON format, often involving decoding bytes to a text string and then parsing it into JSON data.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ binary-string-converter
Binary to String Converter Online tool
Best Binary to String converter Online tool to Convert Binary data to String and Save and Share
๐ŸŒ
Ref45638
ref45638.github.io โ€บ msgpack-converter
MsgPack Converter | MsgPack to JSON Decoder and Encoder (Base64/Hex/Byte Array)
An easy and simple online tool to decode and encode between MsgPack (in base64, hex or byte array) and JSON.
๐ŸŒ
Online Tools
onlinetools.com โ€บ json โ€บ convert-json-to-base64
Convert JSON to Base64 โ€“ Online JSON Tools
Simple, free, and easy-to-use online tool that converts JSON to base64. Just upload your JSON here and you'll instantly get base64.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ base64-to-json
Base64 to JSON Converter: Best and Free
Online Base64 to JSON converter to convert Base64 encoded data to JSON data.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-parser
JSON Parser Online to parse JSON
Secure JSON Parser is an online JSON Parser tool to Parse, Decode and Visualize JSON data in Tree view. JSON Parser online updated in 2022.