I am trying to convert the output of my function which returns a list into a JSON object.
The function outputs the following the list = [b'E28011600000208', b'E28023232083', b'3000948484']
I would like to create a JSON object that has the following attributes:
{"tag": ["E28011600000208", "E28023232083", "3000948484"]}
Decoding of a list item was not shown in the similar example, I need help with that if thats the approach to solving this problem.
The function that I am calling is as follows :
reader.read(timeout=500)
Performs a synchronous read, and then returns a list of TagReadData objects resulting from the search. If no tags were found then the list will be empty.
For example:
print(reader.read())
[b'E2002047381502180820C296', b'0000000000000000C0002403']
In my code I have done the following:
tags = reader.read()
data = json.dumps({tag: tags}, separator=(',','b'))
print (data)
I get the error: raise TypeError(repr(o) + " is not JSON serializable") TypeError: b'3000948484' is not JSON serializable
I tried the solution below to remove the byte string my code is as follows:
tags = reader.read()
tags = list(map(lambda x:x.decode('utf-8'),tags))
data = json.dumps({'tag':tags})
print(data)
I get the error: AttributeError: 'mercury.TagReadData' object has no attribute 'decode'
The output is now JSON but I still have the b' string in my JSON file. I have the following code:
tag = list(map(lambda x: str(x), tag))
data = json.dumps({'tag': tag})
print(data)
The code outputs the following:
{"tag": ["b'30000000321'", "b'300000000'"]}
How do I go about removing the b? By doing str(x) in python 3.5 it was suppose to decode the byte but it didn't.