如何在 NiFi 中使用 JoltTransformationJson 根据文件中另一个键的值将对象添加到 Json 数组

How to Add an Object to a Json Array Depending a value of another Key in the File Using JoltTransformationJson in NiFi

这是我第一次使用JoltTransformationJson,所以我在这方面的知识和经验有限。请帮助我完成这个复杂的项目。

要求: 当 payment.code <>“付费” 时,我必须为文件做以下两件事。

  1. 更改payment.code="拒绝"payment.text="拒绝"
  2. JSON 对象添加到 item.ADJ

payment.code =="已付费"时,不需要更改任何内容。

输入 :

{
  "resourceType": "E",
  "id": "11",
  "identifier": [
    {
      "type": {
        "coding": [
          {
            "system": "sys1",
            "code": "aaa"
          }
        ]
      },
      "value": "212"
    },
    {
      "type": {
        "coding": [
          {
            "system": "sys2",
            "code": "RRR"
          }
        ]
      },
      "value": "367"
    }
  ],
  "status": "active",
  "created": "2021-08-05T02:43:48+00:00",
  "outcome": "complete",
  "item": [
    {
      "sequence": 1,
      "product": {
        "coding": [
          {
            "system": "example",
            "code": "abc",
            "display": "ABC"
          }
        ],
        "text": "ABC"
      },
      "servicedDate": "2021-08-04",
      "quantity": {
        "value": 60
      },
      "ADJ": [
        {
          "category": {
            "coding": [
              {
                "system": "code1",
                "code": "code1",
                "display": "CODE1"
              }
            ],
            "text": "CODE1"
          },
          "amount": {
            "value": 46.45,
            "currency": "USD"
          }
        },
        {
          "category": {
            "coding": [
              {
                "system": "code2",
                "code": "code2",
                "display": "CODE2"
              }
            ],
            "text": "CODE2"
          },
          "amount": {
            "value": 12.04,
            "currency": "USD"
          }
        }
      ]
    }
  ],
  "payment": {
    "type": {
      "coding": [
        {
          "system": "http://payment.com",
          "code": "reversed/cancelled"
        }
      ],
      "text": "cancelled"
    }
  }
}

我的预期输出 :

{
  "resourceType": "E",
  "id": "11",
  "identifier": [
    {
      "type": {
        "coding": [
          {
            "system": "sys1",
            "code": "aaa"
          }
        ]
      },
      "value": "212"
    },
    {
      "type": {
        "coding": [
          {
            "system": "sys2",
            "code": "RRR"
          }
        ]
      },
      "value": "367"
    }
  ],
  "status": "active",
  "created": "2021-08-05T02:43:48+00:00",
  "outcome": "complete",
  "item": [
    {
      "sequence": 1,
      "product": {
        "coding": [
          {
            "system": "example",
            "code": "abc",
            "display": "ABC"
          }
        ],
        "text": "ABC"
      },
      "servicedDate": "2021-08-04",
      "quantity": {
        "value": 60
      },
      "ADJ": [
        {
          "category": {
            "coding": [
              {
                "system": "code1",
                "code": "code1",
                "display": "CODE1"
              }
            ],
            "text": "CODE1"
          },
          "amount": {
            "value": 46.45,
            "currency": "USD"
          }
        },
        {
          "category": {
            "coding": [
              {
                "system": "code2",
                "code": "code2",
                "display": "CODE2"
              }
            ],
            "text": "CODE2"
          },
          "amount": {
            "value": 12.04,
            "currency": "USD"
          }
        },
        {// new object I want to insert into
          "category": {
            "coding": [
              {
                "system": "sys_denail",
                "code": "denialreason"
              }
            ],
          "reason": {
            "coding": [
              {
                "system": "https://example.com",
                "code": "A1"
              }
            ],
            "text": "unknown"
          }}
        }
      ]
    }
  ],
  "payment": {
    "type": {
      "coding": [
        {
          "system": "http://payment.com",
          "code": "denied" //change the value to denied
        }
      ],
      "text": "denied" //change the value to denied
    }
  }
}

编辑: 我尝试自己回答第二个案例,待第一个案例回答后评估

欢迎来到 SO,请 ask minimal and reproducible questions,并展示你为未来所做的努力。

您需要的是根据树中每个键名的级别,使用条件逻辑以及带有 & 符号的占位符值。

我已经部分回答了,这将处理您问题的底部。事实上,其余的逻辑(将对象插入数组将是相似的

所以,考虑看看下面的解决方案

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "payment": {
        "type": {
          "coding": {
            "*": {
              "*": "&4.&3.&2[&1].&",
              "code": {
                "paid": {
                  "@1": "&6.&5.&4[&3].&2",
                  "@(4,text)": "&6.text"
                },
                "*": {
                  "#denied": "&6.&5.&4[&3].code",
                  "@(4,text)": {
                    "#denied": "&6.text"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

编辑您自己关于添加对象的答案:

您目前在 default 转换规范之后使用 shift 的想法非常好,您可以改写为

[
  {
    "operation": "default",
    "spec": {
      "temp_deny": {
        "denialreason": {
          "category": {
            "coding": [
              {
                "system": "sys_denail",
                "code": "denialreason"
              }
            ],
            "reason": {
              "coding": [
                {
                  "system": "https://example.com",
                  "code": "A1"
              }
            ],
              "text": "unknown"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "item": {
        "*": {
          "*": "&2[&1].&",
          "ADJ": {
            "@": "&3[&2].&",
            "@(4,temp_deny)": "&3[&2].&"
          }
        }
      }
    }
  }
]