My idea is to run something like the following:

flowFile = session.get()
if(!flowFile) return

jsonSchema = """
{
// define your schema here
}
"""

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)
     // inspect the validation exception and use it to determine if you need to 
escape quotes, add empty strings for missing values, etc.
   }
 } as InputStreamCallback)

session.transfer(flowFile, REL_SUCCESS)


Another option would be to use the JsonParserLax [1] option in Groovy to read 
the incoming JSON, unmarshal it to a Groovy map, apply a series of common 
transformations (you’ll have more control using native Groovy types rather than 
manipulating a long string), and then marshal it back to well-formed JSON. This 
might be an easier method than what I originally described above.

[1] http://groovy-lang.org/json.html#_parser_variants 
<http://groovy-lang.org/json.html#_parser_variants>

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

> On May 18, 2016, at 1:44 PM, idioma <[email protected]> wrote:
> 
> Andy,
> thank you so much for this, it sounds like a very interesting idea. To
> clarify, are you suggesting to modify the existing Json validator so that it
> can read the invalid Json? Will I then be able to pass it successfully to
> EvaluateJsonPath? Have I understood it correctly?
> 
> Thank you,
> 
> I.
> 
> 
> 
> --
> 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-tp10412p10505.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