The JsonPathReader Controller Service, parses FlowFiles that are in the JSON format. User-defined properties specify how to extract all relevant fields from the JSON in order to create a Record. The Controller Service will not be valid unless at least one JSON Path is provided. Unlike the JsonTreeReader Controller Service, this service will return a record that contains only those fields that have been configured via JSON Path.

If the root of the FlowFile's JSON is a JSON Array, each JSON Object found in that array will be treated as a separate Record, not as a single record made up of an array. If the root of the FlowFile's JSON is a JSON Object, it will be evaluated as a single Record.

Supplying a JSON Path is accomplished by adding a user-defined property where the name of the property becomes the name of the field in the Record that is returned. The value of the property must be a valid JSON Path expression. This JSON Path will be evaluated against each top-level JSON Object in the FlowFile, and the result will be the value of the field whose name is specified by the property name. If any JSON Path is given but no field is present in the Schema with the proper name, then the field will be skipped.

This Controller Service must be configured with a schema. Each JSON Path that is evaluated and is found in the "root level" of the schema will produce a Field in the Record. I.e., the schema should match the Record that is created by evaluating all of the JSON Paths. It should not match the "incoming JSON" that is read from the FlowFile.

Schemas and Type Coercion

When a record is parsed from incoming data, it is separated into fields. Each of these fields is then looked up against the configured schema (by field name) in order to determine what the type of the data should be. If the field is not present in the schema, that field is omitted from the Record. If the field is found in the schema, the data type of the received data is compared against the data type specified in the schema. If the types match, the value of that field is used as-is. If the schema indicates that the field should be of a different type, then the Controller Service will attempt to coerce the data into the type specified by the schema. If the field cannot be coerced into the specified type, an Exception will be thrown.

The following rules apply when attempting to coerce a field value from one data type to another:

If none of the above rules apply when attempting to coerce a value from one data type to another, the coercion will fail and an Exception will be thrown.

Schema Inference

While NiFi's Record API does require that each Record have a schema, it is often convenient to infer the schema based on the values in the data, rather than having to manually create a schema. This is accomplished by selecting a value of "Infer Schema" for the "Schema Access Strategy" property. When using this strategy, the Reader will determine the schema by first parsing all data in the FlowFile, keeping track of all fields that it has encountered and the type of each field. Once all data has been parsed, a schema is formed that encompasses all fields that have been encountered.

A common concern when inferring schemas is how to handle the condition of two values that have different types. For example, consider a FlowFile with the following two records:

[{
    "name": "John",
    "age": 8,
    "values": "N/A"
}, {
    "name": "Jane",
    "age": "Ten",
    "values": [ 8, "Ten" ]
}]

It is clear that the "name" field will be inferred as a STRING type. However, how should we handle the "age" field? Should the field be an CHOICE between INT and STRING? Should we prefer LONG over INT? Should we just use a STRING? Should the field be considered nullable?

To help understand how this Record Reader infers schemas, we have the following list of rules that are followed in the inference logic:

Caching of Inferred Schemas

This Record Reader requires that if a schema is to be inferred, that all records be read in order to ensure that the schema that gets inferred is applicable for all records in the FlowFile. However, this can become expensive, especially if the data undergoes many different transformations. To alleviate the cost of inferring schemas, the Record Reader can be configured with a "Schema Inference Cache" by populating the property with that name. This is a Controller Service that can be shared by Record Readers and Record Writers.

Whenever a Record Writer is used to write data, if it is configured with a "Schema Cache," it will also add the schema to the Schema Cache. This will result in an identifier for that schema being added as an attribute to the FlowFile.

Whenever a Record Reader is used to read data, if it is configured with a "Schema Inference Cache", it will first look for a "schema.cache.identifier" attribute on the FlowFile. If the attribute exists, it will use the value of that attribute to lookup the schema in the schema cache. If it is able to find a schema in the cache with that identifier, then it will use that schema instead of reading, parsing, and analyzing the data to infer the schema. If the attribute is not available on the FlowFile, or if the attribute is available but the cache does not have a schema with that identifier, then the Record Reader will proceed to infer the schema as described above.

The end result is that users are able to chain together many different Processors to operate on Record-oriented data. Typically, only the first such Processor in the chain will incur the "penalty" of inferring the schema. For all other Processors in the chain, the Record Reader is able to simply lookup the schema in the Schema Cache by identifier. This allows the Record Reader to infer a schema accurately, since it is inferred based on all data in the FlowFile, and still allows this to happen efficiently since the schema will typically only be inferred once, regardless of how many Processors handle the data.

Examples

As an example, consider a FlowFile whose content contains the following JSON:

[{
    "id": 17,
    "name": "John",
    "child": {
        "id": "1"
    },
    "siblingIds": [4, 8],
    "siblings": [
        { "name": "Jeremy", "id": 4 },
        { "name": "Julia", "id": 8}
    ]
  },
  {
    "id": 98,
    "name": "Jane",
    "child": {
        "id": 2
    },
    "gender": "F",
    "siblingIds": [],
    "siblings": []
  }]
		

And the following schema has been configured:

{
	"namespace": "nifi",
	"name": "person",
	"type": "record",
	"fields": [
		{ "name": "id", "type": "int" },
		{ "name": "name", "type": "string" },
		{ "name": "childId", "type": "long" },
		{ "name": "gender", "type": "string" },
		{ "name": "siblingNames", "type": {
			"type": "array",
			"items": "string"
		}}
	]
}
        

If we configure this Controller Service with the following user-defined properties:

Property Name Property Value
id $.id
name $.name
childId $.child.id
gender $.gender
siblingNames $.siblings[*].name

In this case, the FlowFile will generate two Records. The first record will consist of the following key/value pairs:

Field Name Field Value
id 17
name John
childId 1
gender null
siblingNames array of two elements: Jeremy and Julia

The second record will consist of the following key/value pairs:

Field Name Field Value
id 98
name Jane
childId 2
gender F
siblingNames empty array