Skip to content

JSONToTuple Operator

Rohit Wagle edited this page Apr 29, 2014 · 13 revisions

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.

##Namespace

com.ibm.streamsx.json

##Parameters 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.

##Examples ###Simple Conversion 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 Nested attributes are handled the same way In the following example, the ParsedS stream will have name = "John", age = 20, address = { NY, 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) {
        }

Clone this wiki locally