[英]How to convert one json format to another using python module
How to convert one json format to another json using python module.如何使用 python 模块将一种 json 格式转换为另一种 json 格式。 I have one json object.我有一台 json object。 Want to take the keys and values.想取键和值。 How to achieve.如何实现。
Thanks in advance.提前致谢。
Input json:输入 json:
{
"A": {
"sensitive": false,
"type": "object",
"value": {
"X": "some value1",
"Y": "some value2"
}
},
"B": {
"sensitive": false,
"type": "object",
"value": [
"1": "some value1",
"2": "some value2"
]
},
"C": {
"sensitive": false,
"type": "object",
"value": {
"ABC": "some value1",
"XYZ": "some value2"
}
}
}
Expected output:预期 output:
{
"A": {
"value": {
"X": "some value1",
"Y": "some value2"
}
},
"B": {
"value": [
"1": "some value1",
"2": "some value2"
]
},
"C": {
"value": {
"ABC": "some value1",
"XYZ": "some value2"
}
}
}
You can iterate over your json-dict and delete particular keys您可以遍历您的 json-dict 并删除特定的键
for key in json_object:
del json_object[key]["sensitive"]
del json_object[key]["type"]
You can do this using a dict comprehension which is less destructive than deleting keys:您可以使用比删除键更具破坏性的 dict 理解来执行此操作:
{k:{'value': v['value']} for k, v in data.items()}
PS: your example data is corrupt and throws errors in Python please check. PS:您的示例数据已损坏并在 Python 中引发错误,请检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.