Reputation: 1132
In my project i have a binary like this:
data1 = b'"[5, \\"WEB99\\", \\"Test\\", true, \\"2018-08-12\\"]"'
i would extract some informations (without \), for example date1[0] -> 5 or data1[4] -> "2018-08-12"
For doing this i try to convert my binary in a dict using json like this:
data1 = data1.decode('utf-8')
then
d1 = json.dumps(data1, default='utf-8')
but the result is a mess:
'"\"[5, \\\"WEB99\\\", \\\"Test\\\", true, \\\"2018-08-12\\\"]\""'
how can i convert in a pythonic way my binary data in a format for extracting values?
Thanks in advance
Upvotes: 0
Views: 3372
Reputation: 2848
You should use loads
instead of dumps
. However, in your case, I used loads
2 times.
data1 = b'"[5, \\"WEB99\\", \\"Test\\", true, \\"2018-08-12\\"]"'
your_list = json.loads(json.loads(data1.decode('utf-8')))
Upvotes: 1
Reputation: 2077
You can do this first:
data1 = data1.decode('utf-8')
then
a = json.loads(json.loads(data1,encoding='utf-8'))
[5, 'WEB99', 'Test', True, '2018-08-12']
at this point you have a list and could extract values as you want:
a[3]
True
Upvotes: 2