我有一个非常复杂的JSON文件,我建议您复制粘贴here以获得更好的可视化效果。
{
"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"
}我不需要所有这些信息,我希望我的数据格式看起来更简单,如下所示:
[
{
"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语言中不必要的单词(用" "替换它们),但它把它搞得一团糟。我还试图将其作为数据帧打开并删除列,但还有许多嵌套的东西我也想删除。
发布于 2021-11-15 12:11:00
您可以编写一个简单的Python脚本来转换此格式:
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)https://stackoverflow.com/questions/69973977
复制相似问题