Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deal a data inside dict&list&string?

I got a json data that some value I need is hiding in a string, but Im not sure is there a good way to pick it out.

the data I need is an url inside the string but conclude list and dict like:

data_2 = content["a"]["b"][0]["value"]
print(type(data_2))
>>>> <class 'str'>
print(data_2)
>>>> [{"id":"123","name":"John","url":"https://www.data.com/"}]

and I have to pick up the value of url.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

strip() doesnt help btw, Im still trying.

the thing is kinda like

{"content":{"custom1":[
{
    "value":"[{"id":"123","name":"John","url":"https://www.data.com/"}]"
}
]}}

>Solution :

Because you have shown data_2 to be String, the objects cannot be accessed directly. The json module can be used to first convert the String, then access first & only element of the list (a Dictionary( and then the url from the Dictionary.

import json

data_2 = '[{"id":"123","name":"John","url":"https://www.data.com/"}]'

data_3 = json.loads(data_2)

print(data_3[0]['url']) 

gives

https://www.data.com/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading