Arjun Arora
Arjun Arora

Reputation: 11

How can I extract the inner string elements from a json using python?

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.

Upvotes: 1

Views: 67

Answers (2)

Vishal Upadhyay
Vishal Upadhyay

Reputation: 811

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
###############

Upvotes: 0

deadshot
deadshot

Reputation: 9071

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]

Upvotes: 1

Related Questions