Python 如何从给定的json文件生成自己的json解析器?

Python 如何从给定的json文件生成自己的json解析器?,python,json,Python,Json,我一直在尝试从给定的json文件生成自己的json解析器。给定的json如下所示: { "id": "Z3PvTW", "title": "TESTING", "theme": { "id": "xwizbR", "font": "Oswald", "name": "Plain Blue (copy)", "colors": { "question": "#3D3D3D",

我一直在尝试从给定的json文件生成自己的json解析器。给定的json如下所示:

 {
    "id": "Z3PvTW",
    "title": "TESTING",
    "theme": {
        "id": "xwizbR",
        "font": "Oswald",
        "name": "Plain Blue (copy)",
        "colors": {
            "question": "#3D3D3D",
            "answer": "#000000",
            "button": "#000000",
            "background": "#FFFFFF"
        },
        "has_transparent_button": false,
        "visibility": "private"
    },
    "workspace": {
        "href": "https:test"
    },
    "settings": {
        "is_public": true,
        "is_trial": false,
        "language": "en",
        "progress_bar": "proportion",
        "show_progress_bar": true,
        "show_typeform_branding": true,
        "meta": {
            "allow_indexing": false
        }
    },
    "welcome_screens": [{
        "ref": "a13820db-af60-40eb-823d-86cf0f20299b",
        "title": "yes",
        "properties": {
            "show_button": true,
            "button_text": "Start"
        }
    }],
    "thankyou_screens": [{
        "ref": "default_tys",
        "title": "Done! Your information was sent perfectly.",
        "properties": {
            "show_button": false,
            "share_icons": false
        }
    }],
    "fields": [{
        "id": "kxWycKljdtBq",
        "title": "FIRST NAME",
        "ref": "27f403f7-8c5b-4e18-b19d-1501e8f137ee",
        "validations": {
            "required": true
        },
        "type": "short_text"
    }, {
        "id": "WEXCnZ7EAFjN",
        "title": "LAST NAME",
        "ref": "a6bf6d83-ee37-4870-b6c5-779822290cde",
        "validations": {
            "required": true
        },
        "type": "short_text"
    }, {
        "id": "ButwoV1bTge5",
        "title": "EMAIL ADDRESS",
        "ref": "8860a4cf-71ec-4bfa-a2c7-934fd405f200",
        "properties": {
            "description": "hehe"
        },
        "validations": {
            "required": true
        },
        "type": "email"
    }, {
        "id": "kzz9Bph353rg",
        "title": "ADDRESS",
        "ref": "e65a4c34-fd2e-4d47-b546-ac2d70679004",
        "validations": {
            "required": true
        },
        "type": "short_text"
    }, {
        "id": "AzZsa4HT2g7g",
        "title": "ADDRESS LINE 2",
        "ref": "35a7c7eb-1617-45a4-b5fa-36b4c6dabfb6",
        "validations": {
            "required": false
        },
        "type": "short_text"
    }, {
        "id": "u5EKtgbNramz",
        "title": "POSTAL\u002FZIP CODE",
        "ref": "a9bb3c05-0c86-4efb-85c1-7e3a4a42f3ec",
        "validations": {
            "required": true
        },
        "type": "short_text"
    }, {
        "id": "q1AIcLze6SdV",
        "title": "CITY",
        "ref": "aead9286-0dff-42f2-8e66-95fffe8711ab",
        "validations": {
            "required": true
        },
        "type": "short_text"
    }, {
        "id": "Dazspa7NoUI1",
        "title": "STATE\u002FPROVINCE\u002FREGION",
        "ref": "eefcc10a-ad87-4f73-be2a-fb286eb5be06",
        "validations": {
            "required": false
        },
        "type": "short_text"
    }, {
        "id": "u26XWl568uQI",
        "title": "COUNTRY",
        "ref": "98ba3e50-c1e2-424f-9487-2c25c7eccaba",
        "properties": {
            "alphabetical_order": false,
            "randomize": false,
            "choices": [{
                "label": "Afghanistan"
            }, {
                "label": "Albania"
            }, {
                "label": "Cambodia"
            }, {
                "label": "Sweden"
            }]
        },
        "validations": {
            "required": true
        },
        "type": "dropdown"
    }, {
        "id": "q9PZyyjeRrGx",
        "title": "Fruit",
        "ref": "805ec00a-b179-4fcb-9ebb-651409ea6751",
        "properties": {
            "alphabetical_order": false,
            "randomize": false,
            "choices": [{
                "label": "Apple"
            }, {
                "label": "Penut"
            }]
        },
        "validations": {
            "required": false
        },
        "type": "dropdown"
    }],
    "_links": {
        "display": "https:test"
    }
 }
这很容易“刮取”,但我确实希望将其转换为拥有自己的json解析器,该解析器应该是使用上述值的输出,以便:

{
  "signature": "1234567" #random numbers,
  "form_id": "Z3PvTW",
  "landed_at": 1580244308 #epoch time,
  "answers": [
    {
      "field": {
        "id": "kxWycKljdtBq", #From the first fields list
        "type": "short_text"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "WEXCnZ7EAFjN",
        "type": "short_text"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "ButwoV1bTge5",
        "type": "email"
      },
      "type": "email",
      "email": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "kzz9Bph353rg",
        "type": "short_text"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "AzZsa4HT2g7g",
        "type": "short_text"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "u5EKtgbNramz",
        "type": "short_text"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "q1AIcLze6SdV",
        "type": "short_text"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "u26XWl568uQI",
        "type": "dropdown"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    },
    {
      "field": {
        "id": "q9PZyyjeRrGx",
        "type": "dropdown"
      },
      "type": "text",
      "text": #will create own config.json file to add a value here
    }
  ]
}
所以我的想法是使用append()函数创建一个空列表,在其中添加内容:

testList = []
for test in json.loads(data).get('fields'): #data is the first given json as I posted at top
        testList["answers"].append({'id':'{}'.format(test.get('id'))})
但我马上就发现一个错误,说:

    testList["answers"].append({'id':'{}'.format(test.get('id'))})
TypeError: list indices must be integers or slices, not str
所以我不太确定我是用正确的方法做的,还是有更简单的方法来做,然后我想做什么


我非常感谢所有关于如何将第一个json转换为第二个json的帮助。好吧,这里的错误是明确的。因为您将testList定义为一个列表,所以字符串索引对它不起作用

一种可能的解决方案是使用字典。例如:

with open('yourfile.json') as file:
        data = json.load(file)

testList = {"answers":[]}
for test in data['fields']: #data is the first given json as I posted at top
        testList["answers"].append({'id':'{}'.format(test.get('id'))})

这将把与键
answers
关联的列表放入有意义的
testList

Ahhh中,因此在这种情况下是有意义的。我会试着多玩一点,看看它是否如我所愿