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
86 views
in Technique[技术] by (71.8m points)

Create JSON object in Python

I have json data as the following list.

"keyword":['agr','case','coffee','tea']

I need to convert this data into the following format in Python.

  {
            "typeName": "keyword",
            "multiple": true,
            "typeClass": "compound",
            "value": [
              {
                "keywordValue": {
                  "typeName": "keywordValue",
                  "multiple": false,
                  "typeClass": "primitive",
                  "value": "agr"
                },
                "keywordValue": {
                  "typeName": "keywordValue",
                  "multiple": false,
                  "typeClass": "primitive",
                  "value": "case"
                }...

                },
                "keywordVocabulary": {
                  "typeName": "keywordVocabulary",
                  "multiple": false,
                  "typeClass": "primitive",
                  "value": "vocab"
                }
              }
            ]
          }

Here is a part from my code. But it takes only the last item of key[] and creates 1 keywordValue object.

for key in keyword:
                    keyword = {
                        'typeName': 'keyword',
                        'multiple': True,
                        'typeClass': 'compound',
                        'value': [
                            {
                                'keywordValue': {
                                    'typeName': 'keywordValue',
                                    'multiple': False,
                                    'typeClass': 'primitive',
                                    'value': key
                                },
                                'keywordVocabulary': {
                                    'typeName': 'keywordVocabulary',
                                    'multiple': False,
                                    'typeClass': 'primitive',
                                    'value': 'test'
                                }
                            }
                        ]
                    }
question from:https://stackoverflow.com/questions/65903854/create-json-object-in-python

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

1 Reply

0 votes
by (71.8m points)

If you're trying to take the values from a json file and load them into Python, the json library helps with this. It reads in the values and saves it as a dictionary.

from json import loads
dictionary = loads(open(input_file, "r").read())

To access the values of "typeName", use typeNameValue = dictionary["typeName"]

If the values of a keyword are multiple dictionaries within it, like value, it will keep creating dictionaries in the values. If you need to access the multiple key in keywordVocabulary, use multiple = dictionary["value"]["keywordVocabulary"]["multiple"]


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

...