Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

Get elements from a json dictionary with Python

i have a following json that i want to read with Python and save table_name and column_filter in a variable in each iteration. My json is like:

import json
dict_config = json.loads('''
    {
        "normal" : [
            {
                "table_name": "TABLE1",
                "column_filter": "TIME_TS"
            },
            {
                "table_name": "TABLE2",
                "column_filter": "TIME_DATE_TS"
            }
        ],
        "normal2" : [
            {
                "table_name": "TABLE3",
                "column_filter": "SOMETHING_TS"
            },
            {
                "table_name": "TABLE4",
                "column_filter": "DOCUMENT_TS"
            }
        ]
    }
''')

for each in dict_config.keys():
    for xx in dict_config[each]
        ...
        table_name=...
        column_filter=.....

How can i easily store value for of table_name and column_filter in normal,normal2,... element? So in each iteration i want to get table_name & column_filter

question from:https://stackoverflow.com/questions/65916450/get-elements-from-a-json-dictionary-with-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are almost correct, just index properly

for each in dict_config:
    for xx in dict_config[each]:
        table_name=xx['table_name']
        column_filter=xx['column_filter']
        print(table_name, column_filter)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...