简体   繁体   English

如何格式化 Json 内容类型?

[英]How to formatting a Json content type?

I need to extract only the content of the operations of the following json:我只需要提取以下json的操作内容:

{"entries":[{"description":"Text transform on 101 cells in column Column 2: value.toLowercase()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 2","expression":"value.toLowercase()","onError":"keep-original","repeat":false,"repeatCount":10,"description":"Text transform on cells in column Column 2 using expression value.toLowercase()"}},{"description":"Text transform on 101 cells in column Column 6: value.toDate()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 6","expression":"value.toDate()","onError":"keep-original","repeat":false,"repeatCount":10,"description":"Text transform on cells in column Column 6 using expression value.toDate()"}}]}

It should look like this:它应该如下所示:

[{"op": "core/text-transform", "engineConfig": {"facets": [], "mode": "row-based"}, "columnName": "Column 2", "expression": "value.toLowercase()", "onError": "keep-original", "repeat": "false", "repeatCount": 10, "description": "Text transform on cells in column Column 2 using expression value.toLowercase()"}, {"op": "core/text-transform", "engineConfig": {"facets": [], "mode": "row-based"}, "columnName": "Column 6", "expression": "value.toDate()", "onError": "keep-original", "repeat": "false", "repeatCount": 10, "description": "Text transform on cells in column Column 6 using expression value.toDate()"}]

I tried to use this code:我尝试使用此代码:

import json
operations = [{"description":"Text transform on 101 cells in column Column 2: value.toLowercase()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 2","expression":"value.toLowercase()","onError":"keep-original","repeat":"false","repeatCount":10,"description":"Text transform on cells in column Column 2 using expression value.toLowercase()"}},{"description":"Text transform on 101 cells in column Column 6: value.toDate()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 6","expression":"value.toDate()","onError":"keep-original","repeat":"false","repeatCount":10,"description":"Text transform on cells in column Column 6 using expression value.toDate()"}}]
new_operations = []

for operation in operations:
  new_operations.append(operation["operation"])

x = json.dumps(new_operations)
print(x)

However, I must manually place quotes in the words like "fake" and also remove the first "entries" part for it to work.但是,我必须手动在“fake”之类的词中加上引号,并删除第一个“条目”部分才能使其正常工作。 Does anyone know how to do it automatically?有谁知道如何自动完成?

IIUC you can do it like this. IIUC你可以这样做。 Read it as json data, extract the parts you want and dump it back to json.将其读取为 json 数据,提取所需的部分并将其转储回 json。

with open('your-json-data.json') as j:
    data = json.load(j)

new_data = []
for dic in data['entries']:
    for key,value in dic.items():
        if key == 'operation':
            dic = {k:v for k,v in value.items()}
            new_data.append(dic)

x = json.dumps(new_data)
print(x)

Output:输出:

[
    {
        "op":"core/text-transform",
        "engineConfig":{
            "facets":[              
            ],
            "mode":"row-based"
        },
        "columnName":"Column 2",
        "expression":"value.toLowercase()",
        "onError":"keep-original",
        "repeat":false,
        "repeatCount":10,
        "description":"Text transform on cells in column Column 2 using expression value.toLowercase()"
    },
    {
        "op":"core/text-transform",
        "engineConfig":{
            "facets":[                
            ],
            "mode":"row-based"
        },
        "columnName":"Column 6",
        "expression":"value.toDate()",
        "onError":"keep-original",
        "repeat":false,
        "repeatCount":10,
        "description":"Text transform on cells in column Column 6 using expression value.toDate()"
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM