我正在使用kazaam模块来定义json转换. https://github.com/qntfy/kazaam

以下是我的规则:

"rules": [
        {
            "operation": "shift",
            "spec": {
                "Shift": "instruction.context.example1"
            }
        },
        {
            "operation": "concat",
            "spec": {
                "sources": [
                    {
                        "path": "instruction.context.example1"
                    },
                    {
                        "path": "instruction.context.example2"
                    }
                ],
                "targetPath": "Concat",
                "delim": "_"
            }
        },
        {
            "operation": "coalesce",
            "spec": {
                "Coalesce": [
                    "instruction.context.example3[0].id"
                ]
            }
        }
    ]

适用于此JSON:

{
    "instruction": {
        "context": {
            "example1": "example1 value",
            "example2": "example2 value",
            "example3": [
                {
                    "id": "example3_1_value"
                },
                {
                    "id": "example3_2_value"
                },
                {
                    "id": "example3_3_value"
                }
            ]
        }
    }
}

结果是:

{
    "Shift": "example1 value",
    "Concat": "null_null"
}

因此,第一个操作起作用,第二个操作返回NULL_NULL,第三个操作不出现.

推荐答案

这些规则被一个接一个地apply.您的第一条规则产生的内容将作为第二条规则的输入,依此类推,它们是连锁的.因此,您首先转换生成一个对象:

{
  "Shift": "example1 value"
}

and when the above is given as an input to your second operation - you get the null values, since the fields you're referring to - do no exist.
You could try to change the first rule to:

        {
            "operation": "shift",
            "spec": {
                "Shift": "instruction.context.example1",
                "instruction.context.example1": "instruction.context.example1",
                "instruction.context.example2": "instruction.context.example2"
            }
        },

看看它的效果

Json相关问答推荐

PostgreSQL 12.17从JSON数组提取元素

与错误相关的未定义&Quot;不是有效的JSON

在 VS Code 中将一个正则表达式替换为另一个正则表达式

NiFi 使用嵌套数组将 avro 转换为 JSON 数组格式

使用 REACT 从字段值中读取 JSON 元素

使用 jq 和 awk 拆分大型 JSON 文件

如果值不存在,则将值插入 JSON 数组

JSON 字段的多个名称

如何在 wso2 中组合 2 个 json 有效负载?

JOLT 转换 - 删除 JSON 数组中的空 node

如果 jq 数组中的字符串是对象或字符串,则获取值

如何将 XML 转换为 PsCustomObject 以允许最终导出为 JSON?

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组

如何在 Go 中生成带有排序键的 JSON?

如何判断 Json 对象中是否存在键并获取其值

我们可以使用 JSON 作为数据库吗?

TypeError: b'1' 不是 JSON 可序列化的

如何将有向无环图 (DAG) 存储为 JSON?

有 Json 标签但未导出

如何使用 Gson 将 JSONArray 转换为 List?