[英]Reformatting json using python
I'm receiving json file like below:我收到 json 文件,如下所示:
[
{
"from":"BUS_TYPE_CODE",
"to":"BUS_TYPE_CODE"
}
,
{
"from":"RECORD_TYPE_IDENTIFIER",
"to":"RECORD_TYPE_IDENTIFIER"
}
]
But I want to reformat it like below before sending to external teams for processing, Any help would be highly appreciated.!.但是我想在发送给外部团队进行处理之前像下面这样重新格式化它,任何帮助将不胜感激。!。
[
{
"from":["BUS_TYPE_CODE"],
"to":["BUS_TYPE_CODE"]
}
,
{
"from":["RECORD_TYPE_IDENTIFIER"],
"to":["RECORD_TYPE_IDENTIFIER"]
}
]
This is a possible solution ( json_obj
is your input JSON object):这是一个可能的解决方案(
json_obj
是您的输入 JSON 对象):
new_json_obj = [{k: [v] for k, v in d.items()} for d in json_obj]
import json
with open('nameoffile.json') as json_file:
data = json.load(json_file)
for dictionary in data:
for key, value in dictionary.items():
dictionary[key]=[value]
Than just print it不仅仅是打印它
print(data)
Or write in another file:或者写入另一个文件:
f = open("newfile.json", "w")
f.write(json.dumps(data))
f.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.