Reputation:
I am using the GetTwitter and EvaluateJsonPath processor to extract the hashtags from twitter streaming API, there after I pass the flowfile through a AttributeToJson processor which will create a property named "hashtags" and will append the value of hashtags which is an array like
[{"text":"Ascendant","indices":[0,10]},{"text":"MediumCoeli","indices":[13,25]},{"text":"Cancer","indices":[75,82]},{"text":"Pisces","indices":[103,110]}]
and append it to the JSON Stream, but this will be enclosed in double quotes in the output of the AttributeToJson processor, ex:
"[{\"text\":\"Ascendant\",\"indices\":[0,10]},{\"text\":\"MediumCoeli\",\"indices\":[13,25]},{\"text\":\"Cancer\",\"indices\":[75,82]},{\"text\":\"Pisces\",\"indices\":[103,110]}]"
How do I get this as a JSON array without the double quotes in the output of the AttributeToJson processor.
Upvotes: 3
Views: 1717
Reputation: 41
I'd recommend using the JoltTransformJSON processor (available in NiFi 0.7.0 and higher) which let's you use the Jolt specification language in order to transform JSON. In the example above with Jolt you could create a chained operation that contains the following:
[
{
"operation": "shift",
"spec": {
"entities": {
"hashtags": {
"*": "hashtags"
}
}
}
}
]
In this case the Jolt processor will pull the hashtags array from the entries map and extract the results to flow file content (instead of in attributes) with the appropriate data types maintained. So no additional quotes around text, etc.
Jolt Processor has an Advanced UI so you can test out transformations as well before enabling it within a flow. Below is a snapshot of the example being tested in it's UI (example is using NiFi version 1.3.0 ). The top section is the Jolt specification being tested and the bottom has a test Twitter payload along with the resulting output:
I hope this is helpful! For additional info on JoltTransformJSON check out NiFi Documentation and search for the processor by name on the left navigation menu. Also for info on Jolt visit its website where they have more detailed information on creating Jolt specifications.
Upvotes: 2