Search code examples
jsonapache-nifi

How to use expression language for JSON contents?


I have try to convert csv to JSON.

My csv data like below..,

Top     
Jun        Sdas 123
Jul        gh   456
Aug-16     Rav  789
Jan-16     Trai 767

Now I have used following processor structure..,

GetFile-->InferredAvroSchema-->ConvertCSVToAvro-->ConvertAvroToJson-->PutFile.

Now result of the json below.,

[
  {
    "field_0": "Top",
    "field_1": "",
    "field_2": null
  },
  {
    "field_0": "Jun",
    "field_1": "Sdas",
    "field_2": 123
  },
  {
    "field_0": "Jul",
    "field_1": "gh",
    "field_2": 456
  },
  {
    "field_0": "Aug-16",
    "field_1": "Rav",
    "field_2": 789
  },
  {
    "field_0": "Jan-16",
    "field_1": "Trai",
    "field_2": 767
  }
]

Now I need to add expression language for "field_0" to add "-16" if it not exists in value.

For example:"field_0" in 2nd row having "Jun". Now I need to check whether "-16" is present in field or not, and also add if not exists.

How to add expression language methods in nifi processors into the JSON content?

Anyotherway is possible?

Any help appreciated and accepted?


Solution

  • There are multiple options you can pursue which will provide the desired functionality.

    Using only provided processors, you could use EvaluateJsonPath or RouteOnContent to detect if the desired key and value is present, and then use ReplaceText or possibly JoltTransformJSON to update the content to include your default value. It appears that your flowfiles are very small, so this shouldn't be an issue, but be aware that JoltTransformJSON does not operate in a streaming manner, so with large flowfiles, this would have a negative impact on memory consumption.

    If you're comfortable with Groovy/Ruby/Jython/Javascript/Lua, you could also do this in one operation with ExecuteScript. With Groovy's built-in JSON handling (quickly treat JSON blocks as map objects), you could do detection and default population in a single line and write the flowfile content back out very easily. ExecuteScript is such a useful and versatile tool that it feels like cheating to recommend it for every problem, but in this case, it really may be the easiest solution.