[[+content_image]]
A
A
Alex_8882019-07-19 19:32:38
Python
Alex_888, 2019-07-19 19:32:38

Get the data of the text field, by the value of the data field in json?

I get a string

jsonStok = {'stock': [[{'data': 'ct02', 'text': 'артикул1'}], [{'data': 'ct04', 'text': 'артикул2'}], [{'data': 'ct06', 'text': 'артикул3'}]}

I need by value 'data' , get value 'text'.
Tried to get
for art in jsonStok['stock']:
    if smth['data'] == 'ct02':
      print smth['text']
      break

But nothing works.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
Sergey Gornostaev, 2019-07-19
@Alex_888

def text_by_data(data, value):
    for x in data['stock']:
        for y in x:
            if y['data'] == value:
                return y['text']


print text_by_data(jsonStok, 'ct02')

PS Until the end of support for Python2, less than six months are left, it's high time to switch to the 3rd one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question