Operator JSONToTuple
Converts JSON strings into SPL tuples.
The tuple structure is expected to match the JSON schema or a subset of attributes can be specified. Only attributes that are present in the Tuple schema and JSON input will be converted. All other attributes will be ignored.
If an invalid JSON string is found in the input, the operator will fail. This behavior can be overridden by specifying the optional output port or by specifying the ignoreParsingError parameter. Attributes from the input stream that match those in the output stream will be automatically copied over. However, if they also exist in the JSON input, their assigned value will be of that specified in the JSON. Null values in JSON arrays are ignored. Null values for all other attributes will result in default initialized output attributes.
- BLOB, MAP and COMPLEX attribute types are not supported in the output tuple schema at this time and will be ignored.
Examples
use com.teracloud.streams.json::*;
public composite Main {
type
PersonSchema = rstring name, int32 age, list<rstring> relatives;
graph
// read the sample file containing one json string per line
stream<Json> JsonStream = FileSource() {
param
file : getThisToolkitDir()+"/opt/sample.json";
format : line;
}
// Read and parse the json into a SPL tuple
// Note that the JSONTuTuple operator will match the tuple attributes
// to their respective JSON ones. Attribute names have to match exactly
stream<PersonSchema> ParsedStream = JSONToTuple(JsonStream) {
}
// Json array example
stream<Json> InputS = Beacon() {
param iterations : 1u;
output InputS : jsonString = '[{"name" : "John"},{}, {"name" : "Kate"} ]';
}
stream<list<rstring name> mylist> OutputS = JSONToTuple(InputS) {
param
targetAttribute : "mylist"; // Must be specified for tuple or collection types
}
}
Summary
- Ports
- This operator has 1 input port and 2 output ports.
- Windowing
- This operator does not accept any windowing configurations.
- Parameters
- This operator supports 6 parameters.
Optional: ignoreParsingError, inputAttribute, jsonStringAttribute, jsonStringOutputAttribute, prefixToIgnore, targetAttribute
- Metrics
- This operator does not report any metrics.
Properties
- Implementation
- Java
- Ports (0)
- Properties
-
- Optional: false
- ControlPort: false
- WindowingMode: NonWindowed
- WindowPunctuationInputMode: Oblivious
- Assignments
- Java operators do not support output assignments.
Optional: ignoreParsingError, inputAttribute, jsonStringAttribute, jsonStringOutputAttribute, prefixToIgnore, targetAttribute
- ignoreParsingError
-
Ignore any JSON parsing errors.If the optional output port is enabled, then this parameter is ignored. JSON that cannot be parsed is sent on the optional output port. Default is false where the operator will fail if the JSON cannot be parsed.
- Properties
-
- Type: boolean
- Cardinality: 1
- Optional: true
- inputAttribute
-
The input stream attribute (not the name of the attribute) which contains the input JSON string. This attribute must be of rstring or ustring type. Default is the sole input attribute when the schema has one attribute otherwise jsonString. Replaces parameter jsonStringAttribute.
- Properties
-
- Type: rstring
- Cardinality: 1
- Optional: true
- ExpressionMode: Attribute
- jsonStringAttribute
-
Deprecated. Use inputAttribute instead. Name of the input stream attribute which contains the JSON string. This attribute must be of rstring or ustring type. Default is the sole input attribute when the schema has one attribute otherwise jsonString.
- Properties
-
- Type: rstring
- Cardinality: 1
- Optional: true
- jsonStringOutputAttribute
-
Name of the output stream attribute which should be populated with the incoming JSON string. This attribute must be of rstring or ustring type. Default is to ignore.
- Properties
-
- Type: rstring
- Cardinality: 1
- Optional: true
- prefixToIgnore
-
Specifies a string that, if present, is removed from the start of an attribute name.You can use this method for JSON that contains elements or attributes with SPL or C++ keywords.For example:
stream <rstring __graph> A = JSONToTuple(Input) { param ignorePrefix : "__"; }This example accepts JSON of the following form:
{"graph" : "value"}Since graph is an SPL keyword, stream<rstring graph> A = JSONToTuple is not valid SPL.
- Properties
-
- Type: rstring
- Cardinality: 1
- Optional: true
- targetAttribute
-
Name of the output stream attribute which should be considered as the root of the JSON tuple to be populated. Note that this can only point to a tuple type or collection type attributes (list, set etc). If it points to a list, set etc type attribute, the input JSON is expected to be an array. Default is the output tuple root.
- Properties
-
- Type: rstring
- Cardinality: 1
- Optional: true
- Operator class library