首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JSON中删除不必要的部分

从JSON中删除不必要的部分
EN

Stack Overflow用户
提问于 2021-11-15 11:51:02
回答 1查看 32关注 0票数 1

我有一个非常复杂的JSON文件,我建议您复制粘贴here以获得更好的可视化效果。

代码语言:javascript
复制
{
  "data":[
    {
      "title":"Title1",
      "paragraphs":[
        {
          "context":"Context1",
          "qas":[
            {
             "answers":[
               {
                "answer_start":515,
                "text":"String text"
               }
              ],
             "question": "Why something something?",
             "id":"5733be284776f41900661182"
             },
             {
             "answers":[
               {
                "answer_start":505,
                "text":"String something text"
               }
              ],
             "question": "Why?",
             "id":"5733be284776f4190066345"
             }
            ]
           },
           {
          "context":"Context2",
          "qas":[
            {
             "answers":[
               {
                "answer_start":515,
                "text":"String text"
               }
              ],
             "question": "Why something something?",
             "id":"5733be284776f41900661182"
             },
             {
             "answers":[
               {
                "answer_start":505,
                "text":"String something text"
               }
              ],
             "question": "Why?",
             "id":"5733be284776f4190066345"
             }
            ]
           }
          ]
         },
         {
         "title":"Title2",
      "paragraphs":[
        {
          "context":"Context10",
          "qas":[
            {
             "answers":[
               {
                "answer_start":585,
                "text":"String text"
               }
              ],
             "question": "Why something something?",
             "id":"5733be284776f41900661682"
             },
             {
             "answers":[
               {
                "answer_start":545,
                "text":"String something text"
               }
              ],
             "question": "Why?",
             "id":"5733be284776f41900663"
             }
            ]
           },
           {
          "context":"Context7",
          "qas":[
            {
             "answers":[
               {
                "answer_start":525,
                "text":"String text"
               }
              ],
             "question": "Why something something?",
             "id":"5733be284776f41982"
             },
             {
             "answers":[
               {
                "answer_start":595,
                "text":"String something text"
               }
              ],
             "question": "Why?",
             "id":"5733be284776f419005"
             }
            ]
           }
          ]
         }
         ],
          "version":"1.1"
         }

我不需要所有这些信息,我希望我的数据格式看起来更简单,如下所示:

代码语言:javascript
复制
[
 {
   "id": "5733be284776f41900661182",
   "context": "Context1",
   "question": "Why something something?",
   "answers": {"text": ["String text"]}
 },
 {
    "id": "5733be284776f4190066345",
    "context": "Context1",
    "question": "Why?",
    "answers": {"text": ["String something text"]}
 },
 {
   "id": "5733be284776f41900661182",
   "context": "Context2",
   "question": "Why something something?",
   "answers": {"text": ["String text"]}
 },
 {
    "id": "5733be284776f4190066345",
    "context": "Context2",
    "question": "Why?",
    "answers": {"text": ["String something text"]}
 },
 {
   "id": "5733be284776f41900661682",
   "context": "Context10",
   "question": "Why something something?",
   "answers": {"text": ["String text"]}
 },
 {
    "id": "5733be284776f41900663",
    "context": "Context10",
    "question": "Why?",
    "answers": {"text": ["String something text"]}
 },
 {
   "id": "5733be284776f41982",
   "context": "Context7",
   "question": "Why something something?",
   "answers": {"text": ["String text"]}
 },
 {
    "id": "5733be284776f419005",
    "context": "Context7",
    "question": "Why?",
    "answers": {"text": ["String something text"]}
 }                                                                                                                                  
]

我不关心段落,标题,答案开始等,我更希望上下文是分开的。有没有办法把这样的JSON变成更简单的东西呢?我试图删除Python语言中不必要的单词(用" "替换它们),但它把它搞得一团糟。我还试图将其作为数据帧打开并删除列,但还有许多嵌套的东西我也想删除。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-15 12:11:00

您可以编写一个简单的Python脚本来转换此格式:

代码语言:javascript
复制
import json

with open('file.json', 'r') as fh:
    data = json.load(fh)

result = []

for article in data["data"]:
    for paragraph in article["paragraphs"]:
        for qa in paragraph["qas"]:
            answers = {"text": [answer["text"] for answer in qa["answers"]]}
            result.append({
                "id": qa["id"], 
                "context": paragraph["context"], 
                "question": qa["question"], 
                "answers": answers
            })

with open('output.json', 'w') as fh:
    json.dump(result, fh)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69973977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档