Ilaria,

Matt just wrote a JSON validator Groovy script for another answer on the 
mailing list. I’ve included it below. You can modify this to perform validation 
on the incoming JSON as defined by your custom schema, and then applying the 
various fixes. I’m not familiar with the library Matt is suggesting, but I 
would imagine the validate method throws specific errors or has optional return 
values to help specify what needs to be fixed.

As for extracting the attribute itself into the script, 
session.getAttribute(flowfile, “attributeName”) is the easiest way. In the 
script Matt wrote below, the JSON is the flowfile content, not an attribute, so 
he uses session.read(flowfile, Closure which accepts InputStream and is cast as 
StreamCallback) to declare an inline block which reads the flowfile content and 
operates on it.

Hope this helps.

> In the meantime, here's a Groovy script you could use in
> ExecuteScript, just need to download the two JAR dependencies ([2] and
> [3]) and add them to your Module Directory property.
> 
> import org.everit.json.schema.Schema
> import org.everit.json.schema.loader.SchemaLoader
> import org.json.JSONObject
> import org.json.JSONTokener
> 
> flowFile = session.get()
> if(!flowFile) return
> 
> jsonSchema = """
> {
>  "type": "object",
>  "required": ["name", "tags", "timestamp", "fields"],
>  "properties": {
>    "name": {"type": "string"},
>    "timestamp": {"type": "integer"},
>    "tags": {"type": "object", "items": {"type": "string"}},
>    "fields": { "type": "object"}
>  }
> }
> """
> 
> boolean valid = true
> session.read(flowFile, { inputStream ->
>   jsonInput = org.apache.commons.io.IOUtils.toString(inputStream,
> java.nio.charset.StandardCharsets.UTF_8)
>   JSONObject rawSchema = new JSONObject(new JSONTokener(new
> ByteArrayInputStream(jsonSchema.bytes)))
>   Schema schema = SchemaLoader.load(rawSchema)
>   try {
>      schema.validate(new JSONObject(jsonInput))
>    } catch(ve) {
>      log.error("Doesn't adhere to schema", ve)
>      valid = false
>    }
>  } as InputStreamCallback)
> 
> session.transfer(flowFile, valid ? REL_SUCCESS : REL_FAILURE)
> 
> 
> Hope this helps!
> 
> Regards,
> Matt
> 
> [1] https://issues.apache.org/jira/browse/NIFI-1893 
> <https://issues.apache.org/jira/browse/NIFI-1893>
> [2] 
> http://mvnrepository.com/artifact/org.everit.json/org.everit.json.schema/1.3.0
>  
> <http://mvnrepository.com/artifact/org.everit.json/org.everit.json.schema/1.3.0>
> [3] http://mvnrepository.com/artifact/org.json/json/20160212 
> <http://mvnrepository.com/artifact/org.json/json/20160212>

Andy LoPresto
[email protected]
[email protected]
PGP Fingerprint: 70EC B3E5 98A6 5A3F D3C4  BACE 3C6E F65B 2F7D EF69

> On May 17, 2016, at 12:51 PM, idioma <[email protected]> wrote:
> 
> Andy, thanks for sharing this with me, I was not aware of it. Unfortunately,
> for a number of reasons, I will need to use 0.5.1 for the time being, so I
> was looking at existing processors. I have had a go with the combination
> EvaluateJsonPath + UpdateAttribute and although I am not a bit more familiar
> with JsonPath expressions, this approach only works if your Json is already
> coming in as a valid one. I believe, in this case again, I should resort to
> a Groovy script to be run as part of ExecuteScript. If you can think of
> anything else, please drop your suggestion here.
> 
> Thank you so much
> 
> 
> 
> --
> View this message in context: 
> http://apache-nifi-developer-list.39713.n7.nabble.com/Formatting-issues-with-Json-what-is-the-best-approach-in-NiFi-tp10412p10458.html
> Sent from the Apache NiFi Developer List mailing list archive at Nabble.com.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to