1

I have this weird raw JSON input :

{
"URL_IN": "http://localhost/",
"DownloadData": "{\"data\":[{samples:[{t:1586826385724,v:5.000e+000,l:0,s:-1,V:-1},{t:1587576460460,v:0.000e+000,l:0,s:-1,V:-1}]}]}"
}

And I want to access and extract the inner elements, like t, v from samples using Python.

2
  • You want value of t,v... ,right? Commented Apr 30, 2020 at 11:24
  • Yes @VishalUpadhyay Commented Apr 30, 2020 at 11:30

2 Answers 2

1

You can first clean json using regex. For cleaning i am splitting json into two parts url_data and download_data .

First step remove the unnecessary double quotes from download_data this regular expression re.sub('"', '', data[data.index(',') + 1 :]) removes the double quotes.

Next add the double quotes to words using regular expression re.sub("(\w+):", r'"\1":', download_data) this will add double quotes around all the words in the json.

import re
import json
data = '{"URL_IN": "http://localhost/","DownloadData": "{\"data\":[{samples:[{t:1586826385724,v:5.000e+000,l:0,s:-1,V:-1},{t:1587576460460,v:0.000e+000,l:0,s:-1,V:-1}]}]}"}'
url_data = data[:data.index(',') + 1]
download_data = re.sub('"', '', data[data.index(',') + 1 :])
data = url_data + re.sub("(\w+):", r'"\1":',  download_data)
data = json.loads(data)
res = [(x['t'], x['v']) for x in data['DownloadData']['data'][0]['samples']]
t, v = map(list, zip(*res))
print(t, v)

Output:

[1586826385724, 1587576460460] [5.0, 0.0]
Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect ! Thank you so much.
0

Here the main problem what I saw is the value in DownloadData is not in json format, so you need to make it as json.

Code

a={ "URL_IN": "http://localhost/", "DownloadData": "{\"data\":[{samples:[{t:1586826385724,v:5.000e+000,l:0,s:-1,V:-1},{t:1587576460460,v:0.000e+000,l:0,s:-1,V:-1}]}]}" }
i = a['DownloadData']
#converting string to json
i = i.replace("{",'{"').replace("}",'"}').replace(":",'":"').replace(",",'","')
i = i.replace("\"\"",'\"').replace("\"[",'[').replace("\"]",']').replace("\"{",'{').replace("\"}",'}')
i = i.replace("}]}]}","\"}]}]}")
i = i.replace("}\"","\"}")
final_dictionary = json.loads(i) 
for k in final_dictionary['data'][0]['samples']:
    print("t = ",k['t'])
    print("v = ",k['v'])
    print("l = ",k['l'])
    print("s = ",k['s'])
    print("V = ",k['V'])

    print("###############")

Output

t =  1586826385724
v =  5.000e+000
l =  0
s =  -1
V =  -1
###############
t =  1587576460460
v =  0.000e+000
l =  0
s =  -1
V =  -1
###############

1 Comment

Hey Vishal! Thanks for the solution. Yes the problem was the json was not clean enough to work directly.

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.