Answer the question
In order to leave comments, you need to log in
How to parse JSON file with variable data structure?
How to deal with JSON objects when they are of variable type?
For example, I want to find all links in the export of the chat log from TELEGRAM, where "text" is either just a string, or a list, inside of which there is a dictionary.
How to nicely and intelligently access the value of a dictionary that contains a reference?
My problem is that if I try to iterate over "text" as a list, there will be errors because sometimes it doesn't have a list.
You can of course suppress errors, but this is also not correct IMHO.
code sketch with crutches (error suppression, list item hardcoded):
import json
json_str = '''
[
{
"id": 5,
"type": "message",
"date": "2019-01-02T12:05:43",
"from": "nik",
"from_id": 12457887,
"text": "hi!"
},
{
"id": 6,
"type": "message",
"date": "2019-01-02T12:05:45",
"from": "nik2",
"from_id": 22457877,
"text": [
"hi tyt info!",
{
"type": "link",
"text": "https://telegra.ph/film-05-01"
},
""
]
}
]
'''
json_data = json.loads(json_str)
for stroka in json_data:
http_data = stroka['text']
try:
print(http_data[1]['text']) # тут номер [1] элемента со ссылкой в списке думаю может меняться, придется перебирать все элементы?
except:
pass
Answer the question
In order to leave comments, you need to log in
To check types of the necessary data?
for stroka in json_data:
http_data = stroka['text']
if isinstance(http_data, list):
for chunk in http_data:
if isinstance(chunk, dict):
print(chunk.get('text'))
Any one for your task and attiny will do, so everything else will be easy to handle. If you want to use a motor shield, then you will have to take a full-sized uno type.
They are approximately equal in price, they will also be approximately the same in response speed, bluetooth may be a little slower, but this is a moot point, you can’t determine it by eye anyway, everything worked fine on bluetooth.
There is no difference, everything, including the "original" ones, is made in China according to the same schemes, I have had a cheap Chinese pro for a year now that has been threshing around the clock and has not even hung up even once.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question