0

Code used for extraction from JSON

import json
string = json.loads(data)
string['Body']

import base64
base64.b64decode(string['Body'])

bytes_data = base64.b64decode(string['Body'])
str(bytes_data, encoding='utf-8')

I have a following format that is extracted from JSON

"[{"id":"XXXX_U2_170216:XXXX_U2_170216:FBE_23015.Air","values":[{"v":"46","q":192,"t":"2021-10-28T13:47:59.7880096Z"}]},
{"id":"XXXX_U2_170216:XXXX_U2_170216:FBE_23015.Atomise","values":[{"v":"3.1","q":192,"t":"2021-10-28T13:47:59.7880096Z"}]}]"

Any idea about converting it to actual list

[{"id":"XXXX_U2_170216:XXXX_U2_170216:FBE_23015.Air","values":[{"v":"46","q":192,"t":"2021-10-28T13:47:59.7880096Z"}]},
{"id":"XXXX_U2_170216:XXXX_U2_170216:FBE_23015.Atomise","values":[{"v":"3.1","q":192,"t":"2021-10-28T13:47:59.7880096Z"}]}]

things I have tried :

list(bytearray(bytes_data))

for loop - for this output string, but this is a convoluted way to do it.
some more conversion stuff. looking for something that is compact.

2
  • 2
    Please show a minimal reproducible example. What is data? I see no base64 encoded values, so why are you decoding? Where is Body key in your data? Commented Dec 8, 2021 at 4:36
  • The data is decoded part. I dont have problem there. Body is what I am getting in a string format, I am trying to convert to a list. Better if that can be converted to dataframe. Commented Dec 8, 2021 at 4:47

3 Answers 3

1

Reverse engineering your question....

Given a JSON file with base64 data

$ cat /tmp/data.json
{
  "Body": "W3siaWQiOiJYWFhYX1UyXzE3MDIxNjpYWFhYX1UyXzE3MDIxNjpGQkVfMjMwMTUuQWlyIiwidmFsdWVzIjpbeyJ2IjoiNDYiLCJxIjoxOTIsInQiOiIyMDIxLTEwLTI4VDEzOjQ3OjU5Ljc4ODAwOTZaIn1dfSwKeyJpZCI6IlhYWFhfVTJfMTcwMjE2OlhYWFhfVTJfMTcwMjE2OkZCRV8yMzAxNS5BdG9taXNlIiwidmFsdWVzIjpbeyJ2IjoiMy4xIiwicSI6MTkyLCJ0IjoiMjAyMS0xMC0yOFQxMzo0Nzo1OS43ODgwMDk2WiJ9XX1dCg=="
}

When read and extracted

import json
import base64
with open('/tmp/data.json') as f:
    string = json.load(f)
body = string['Body']

Then decoded... a list is returned

import pprint

l = json.loads(base64.b64decode(body)
pprint.pprint(l)
[{'id': 'XXXX_U2_170216:XXXX_U2_170216:FBE_23015.Air',
  'values': [{'v': '46', 'q': 192, 't': '2021-10-28T13:47:59.7880096Z'}]},
 {'id': 'XXXX_U2_170216:XXXX_U2_170216:FBE_23015.Atomise',
  'values': [{'v': '3.1', 'q': 192, 't': '2021-10-28T13:47:59.7880096Z'}]}]
Sign up to request clarification or add additional context in comments.

2 Comments

I'm confused, this is the exact same answer I wrote.
Bharel, apologies. I had skipped that part. Idk if there is way to take two answers. I was interacting with @OneCricketeer. I appreciate help from both of you.
0

Use the built-in json module:

import json
data = json.loads(bytes_data)

It seems like you have a json inside a json, so load it twice:

import json
import base64

string = json.loads(data)
bytes_data = base64.b64decode(string['Body'])
output = json.loads(bytes_data)

4 Comments

OP already knows about json.loads. Where are you getting string['Body'] from the data shown?
@OneCricketeer as far as I understand, he extracted the Body field from the json, and he showed you the output, which is by itself a json.
Seems like a guess, considering there is no print statement shown, but we'll see.
@OneCricketeer Not a guess, he showed you the code he's using to extract the json data, and then the data (which is formatted as a json again). He clearly states that.
0

use json load method like this, suppose you have JSON array and want to convert in LIST then do following

import json
array = '{"Items": ["IPhone", "Earphone", "Powerbackup"]}'
data  = json.loads(array)
print (data['Items'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.