Search code examples
jsonapache-nifi

How to convert Json to string at attribute level?


In my nifi flow, I have read a json object using EvaluateJsonPath processor.

The JSON object looks like below:

[
  {
    "id": "8cbfa4dd-aib4-4f55-b10e-439c9d7f7b4c",
    "parent_id": 208,
    "dat_asset": {
      "name": "gpt4.pdf",
      "scope": "group",
      "source": "upload",
      "filename": "gpt4.pdf",
      "bucket_name": "psci-bis-1lg-jk",
      "source_tags": [
        "test"
      ],
      "content_type": "application/pdf",
      "opp_pid": null,
      "bucket_file_names": "resources/8cbfa4dd-aab4-4f55-b10e-439c9d7f7b4c/data_asset/gpt4.pdf",
      "destFolderPid": ""
    }
  }
]

I want to convert this json object to json string at attribute level. My attribute name is source_data which currently holds above json object


Solution

  • I found the solution in apache nifi documentation- https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#escapejson

    W need to use escapeJson function

    Description: This function prepares the Subject to be inserted into JSON document by escaping the characters in the String using Json String rules. The function correctly escapes quotes and control-chars (tab, backslash, cr, ff, etc.)

    Subject Type: String

    Arguments: No arguments

    Return Type: String

    Examples: If the "message" attribute is 'He didn’t say, "Stop!"', then the Expression ${message:escapeJson()} will return 'He didn’t say, \"Stop!\"'

    After using escapeJson function my json string looks like below:

    "[{\"id\":\"8cbfa4dd-aib4-4f55-b10e-439c9d7f7b4c\",\"parent_id\":208,\"dat_asset\":{\"name\":\"gpt4.pdf\",\"scope\":\"group\",\"source\":\"upload\",\"filename\":\"gpt4.pdf\",\"bucket_name\":\"psci-bis-1lg-jk\",\"source_tags\":[\"test\"],\"content_type\":\"application/pdf\",\"opp_pid\":null,\"bucket_file_names\":\"resources/8cbfa4dd-aab4-4f55-b10e-439c9d7f7b4c/data_asset/gpt4.pdf\",\"destFolderPid\":\"\"}}]"