0

Is there a way to convert a JSON string to a SPL tuple type without using JSONtoTuple Operator? I saw this documentation: https://developer.ibm.com/streamsdev/docs/introducing-the-json-toolkit/ where they have mentioned a native function for converting tuple to json but not json to tuple.

How do I convert a JSON to Tuple inside a Custom operator?

4

1 回答 1

1

version 1.4+ of the JSON toolkit includes functions that you can call from a custom. This version must be downloaded from Github, as it is not yet included in the Streams product.

Download the latest version from Github, which is 1.4.4. Build the toolkit: cd com.ibm.streamsx.json and run ant.

Then you can use the extractJSON function:

public T extractFromJSON(rstring jsonString, T value)

Pass the JSON string you want to parse, and a mutable tuple that will contain the parsed result as parameters.

For example:

composite ExtractFromJSON {
type

 //the target output type. Nested JSON arrays are not supported.
    Nested_t = tuple<rstring name, int32 age, list<rstring> relatives> person, tuple<rstring street, rstring city> address;


graph
    () as Test = Custom() {
        logic
            onProcess : {
                rstring jsonString = '{"person" : {"name" : "John", "age" : 42, "relatives" : ["Jane","Mike"]}, "address" : {"street" : "Allen Street", "city" : "New York City"}}';

                mutable Nested_t nestedTuple = {};
                println( extractFromJSON(jsonString, nestedTuple));
            }
    }
}

This is based on the ExtractFromJSON sample that you can find in the repo on Github.

Hope this helps.

于 2018-02-20T17:19:14.273 回答