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

Viewed 100

This is my first time to use JoltTransformationJson, so I have limited knowledge and experience on that. Please help me with this complicated project.

Request: when the payment.code <> "paid", I have to do the following two things for the file.

  1. to change the payment.code ="denied" and payment.text ="denied"
  2. to add a JSON object to item.ADJ

When the payment.code =="paid", don't need to change anything.

Input :

{
  "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"
    }
  }
}

My Expected Output :

{
  "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
    }
  }
}

Edit : I've tried to answer the second case by myself to be evaluated after the first case is answered

1 Answers

Welcome to SO, please ask minimal and reproducible questions, and show your effort tried for the future.

What you need is to use a conditional logic along with placeholder values with ampersand symbols depending on the levels of each key name within the tree.

I have partially answered, which will handle the bottom part of your question. Indeed the logic for the rest(inserting an object to the array will be similiar)

So, consider having a look at the following solution

[
  {
    "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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Edit(for your own answer related to adding an object):

your current idea of using shift after default transformation spec is pretty good, you can rephrase like

[
  {
    "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].&"
          }
        }
      }
    }
  }
]
Related