-
Notifications
You must be signed in to change notification settings - Fork 19
JSONToTuple Operator
The JSONToTuple operator can be used to convert JSON strings to SPL tuples. The operator inspects the schema of the output stream and converts all matching attributes of the JSON to their SPL counterparts.
In the following example, the ParsedS stream will have name = "John" and age = 20.
stream<rstring jsonString> JsonS = Beacon() {
param
iterations : 1u;
output JsonS : jsonString = "{\"name\" : \"John\", \"age\" : 20}";
}
stream<rstring name, int32 age> ParsedS = JSONToTuple(JsonS) {}
Nested attributes are handled the same way.
In the following example, the ParsedS stream will have name = "John", age = 20, address = { state = "NY", country = "USA" }
type AddressType = rstring state, rstring country;
type PersonType = rstring name, int32 age, AddressType address;
stream<rstring jsonString> JsonS = Beacon() {
param
iterations : 1u;
output JsonS :
jsonString = "{\"name\" : \"John\", \"age\" : 20 , \"address\" : { \"state\" : \"NY\", \"country\" : \"USA\" } }";
}
stream<PersonType> ParsedS = JSONToTuple(JsonS) {}
You need to only specify the attributes you want in your output stream. All other attributes in the JSON will be ignored.
In the following example, only the country attribute is extracted
type AddressType = rstring country;
type PersonType = AddressType address;
stream<rstring jsonString> JsonS = Beacon() {
param
iterations : 1u;
output JsonS :
jsonString = "{\"name\" : \"John\", \"age\" : 20 , \"address\" : { \"state\" : \"NY\", \"country\" : \"USA\" } }";
}
stream<PersonType> ParsedS = JSONToTuple(JsonS) {}
The input string need not be a JSON record. JSON arrays are also supported as input.
In the following example, the incoming JSON string is expected to be a string since the targetAttribute parameter points to a list type. Here MyType can be any SPL type.
Note that the targetAttribute is required to be specified in case the input is a JSON array and it must point to a collection type (list, blist, set, bset).
MyType = rstring name;
stream<rstring jsonString> InputS = Beacon() {
param iterations : 1u;
output InputS : jsonString = "[{\"name\" : \"John\"}, {\"name\" : \"Kate\"} ]";
}
stream<list<MyType> mylist> OutputS = JSONToTuple(InputS) {
param
targetAttribute : "mylist";
}
com.ibm.streamsx.json
Following parameters are supported
jsonStringAttribute
This is an optional parameter that can be used to specify the name of the input stream attribute that contains the JSON string. By default, the operator expects an attribute named "jsonString" to be present in the input stream. This attribute can be either of USTRING or RSTRING type.
jsonStringOutputAttribute
This is an optional parameter that can be used to copy the original JSON string to the output stream. It should contain the name of the output stream attribute, of type USTRING or RSTRING. By default, the input JSON string will not be copied to the output stream.
targetAttribute
This is an optional parameter that can be used to specify the name of an inner attribute of the output stream to be the root of the SPL typle to be populated. By default, the base of the output stream tuple is used to match with the JSON.