0

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.

1 Answers1

0

Python dict should have unique keys. So repeating keys will not work and as a result it will hold just one value. However if you keep the only one key i.e tag and value as list should work.

Having said that, json.dumps or json.loads does not handle the dict object if it contain tuple, byte etc. object. Here in your example the list is byte string which is having JSON (de)serializing problem.

Now if you dont care about the byte string and want to decode to utf-8 which basically convert to string then you can find the solution here.

l = [b'E28011600000208', b'E28023232083', b'3000948484']
l = list(map(lambda x: x.decode('utf-8'), l)))
data = json.dumps({'tag': l})
print(data)
# Out: '{"tag": ["E28011600000208", "E28023232083", "3000948484"]}'

But if you want to keep byte string then look at how to handle while serializeing and desirializing the object using custom json encoder class via extra params cls

json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

pickle is another useful lib to pack/unpack data and it does take care of any objects(python). But it returns packed data as byte string not useful if need to share to 3rd application. Very useful when memcache any python object.

pickle.loads(pickle.dumps(l))
MaNKuR
  • 2,578
  • 1
  • 19
  • 31
  • I tried a variation of your code, but I got an error, please review my main post I've added the information at the bottom. – Harout Simonian Aug 06 '18 at 18:19
  • That's error related to your data....try this.. tag = list(map(lambda x: str(x).decode('utf-8'), tag))... Ensure data consistency... – MaNKuR Aug 07 '18 at 05:52