Arrays 如何从json文件构建以下字典?

Arrays 如何从json文件构建以下字典?,arrays,python-3.x,pandas,dictionary,Arrays,Python 3.x,Pandas,Dictionary,您好,我正在处理一个json文件,该文件如下所示: { "workspace_id": "car-dashboard-en", "name": "Car Dashboard - Sample", "created": "2017-03-20T21:24:41.498Z", "intents": [ { "intent": "about_VA", "created": "2017-03-14T02:02:16.290Z",

您好,我正在处理一个json文件,该文件如下所示:

{
    "workspace_id": "car-dashboard-en",
    "name": "Car Dashboard - Sample",
    "created": "2017-03-20T21:24:41.498Z",
    "intents": [
      {
        "intent": "about_VA",
        "created": "2017-03-14T02:02:16.290Z",
        "updated": "2017-03-14T02:02:16.290Z",
        "examples": [
          {
            "text": "any music reccomendation?",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "can you recommend ood jazz music?",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "cloud you recommend music?",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "your name",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          }
        ],
        "description": null
      },
      {
        "intent": "capabilities",
        "created": "2017-03-14T02:02:16.290Z",
        "updated": "2017-03-14T02:02:16.290Z",
        "examples": [
          {
            "text": "adapt to current weather condition",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "book a flight for NY on sunday",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },

          {
            "text": "can you change lanes",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
dictionary = {'intent_name':[text1,text1,text3,...],'intent_name2':[text1,text2,text3,...],...}
我有一个json,它有一个名为“intent”的部分。这个部分由几个intent组成,每个intent都有一个名为“text”的值列表

我想从这个文件中提取一本字典,如下所示:

{
    "workspace_id": "car-dashboard-en",
    "name": "Car Dashboard - Sample",
    "created": "2017-03-20T21:24:41.498Z",
    "intents": [
      {
        "intent": "about_VA",
        "created": "2017-03-14T02:02:16.290Z",
        "updated": "2017-03-14T02:02:16.290Z",
        "examples": [
          {
            "text": "any music reccomendation?",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "can you recommend ood jazz music?",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "cloud you recommend music?",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "your name",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          }
        ],
        "description": null
      },
      {
        "intent": "capabilities",
        "created": "2017-03-14T02:02:16.290Z",
        "updated": "2017-03-14T02:02:16.290Z",
        "examples": [
          {
            "text": "adapt to current weather condition",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
          {
            "text": "book a flight for NY on sunday",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },

          {
            "text": "can you change lanes",
            "created": "2017-03-14T02:02:16.290Z",
            "updated": "2017-03-14T02:02:16.290Z"
          },
dictionary = {'intent_name':[text1,text1,text3,...],'intent_name2':[text1,text2,text3,...],...}
本词典的关键字是相关意图的名称,值是该意图的每个文本

不幸的是,我是json文件的初学者,所以我尝试了:

import json

with open('workspace-car-dashboard-en.json') as json_file:  
    data = json.load(json_file)
使用以下代码,我提取并打印所有文本值,如下所示:

for element in data['intents']:
    for element2 in element['examples']:
        print(element2['text'])
我得到:

any music reccomendation?
can you recommend ood jazz music?
cloud you recommend music?
do you hate
Do you like
do you love
但是,我无法动态构建字典并对所有文本值进行追加,因此我非常感谢支持以完成这项艰巨的任务。
我无法包含完整的json文件,因为它非常大,所以我希望这个示例足够了。

创建一个新的空目录。添加每个意图名称的键并设置空列表的值。 然后使用创建的示例文本插入循环中的每个文本键值

import json

with open('workspace-car-dashboard-en.json') as json_file:
    data = json.load(json_file)

new_dic = {}
for element in data['intents']:
    new_dic[element['intent']] = []
    for element2 in element['examples']:
        new_dic[element['intent']] += [element2['text']]