qie.parseJSONString¶
Signature: qie.parseJSONString(value, encoding*)
Returns: JSON Message jsonMessage - the associated JSON message object
Parse the input as JSON and return the associated message object.
Note
The value can be either a 'String' or JSON. JSON can be represented by JavaScript objects, arrays and native maps and Java maps, collections, raw arrays or anything that implements the org.mozilla.javascript.Scriptable interface.
Note
The return value CAN NEVER be assigned to the bound-in message object; it should be assigned to a locally declared variable.
Parameters¶
| Type | Name | Description | Default |
|---|---|---|---|
| String or JSON | value | the source to be parsed | |
| String | encoding* | (optional) the character encoding for this message | Source Node encoding |
Example¶
var jsonString = `
{
"name": "Nate",
"age": 30,
"isStudent": false,
"address": {
"street": "123 Main St",
"city": "Somewhere",
"zipcode": "12345"
},
"hobbies": ["reading", "coding", "hiking"]
}
`;
var newJsonMessage = qie.parseJSONString(
jsonString, // value: source JSON string
'UTF-8' // encoding: character encoding (optional)
);
// create message with native JSON
newJsonMessage = qie.parseJSONString({test: "test1", id: 1});
// create message with NativeMap using JSON.stringify()
var myMap = new Map();
myMap.set("test", "test5");
myMap.set("id", 5);
newJsonMessage = qie.parseJSONString(JSON.stringify(Object.fromEntries(myMap)));
// create message with NativeMap passed directly to parseJSONString()
var nativeMap = new Map();
nativeMap.set("test", "test6");
nativeMap.set("id", 6);
newJsonMessage = qie.parseJSONString(nativeMap);
// create message with NativeArray
var nativeArray = ["x", "y", "z"];
newJsonMessage = qie.parseJSONString(nativeArray);
// create message with Java Map
var javaMap = new java.util.LinkedHashMap();
javaMap.put("test", "test7");
javaMap.put("id", 7);
newJsonMessage = qie.parseJSONString(javaMap);
// create message with Java Collection
var javaList = new java.util.ArrayList();
javaList.add("p");
javaList.add("q");
javaList.add("r");
newJsonMessage = qie.parseJSONString(javaList);
// create message with Java array
var javaArray = java.lang.reflect.Array.newInstance(java.lang.String, 3);
javaArray[0] = 'foo';
javaArray[1] = 'bar';
javaArray[2] = 'baz';
newJsonMessage = qie.parseJSONString(javaArray);