Sidebar
0 votes
1.9K views
by brandon-w-8204 (34.9k points)

2 Answers

0 votes

Here is a simple example that creates an array of strings:

var i;
message.formatJSON(3);
message.setJSONArray('arrayExample');
// Populate New Array
for (i = 0; i < 10; i++) {
   message.addStringToJSONArray('arrayExample', 'ABCD' + i);
}

// Debug Whole Array
qie.debug(message.getNode('arrayExample'));

// Cycle Each Object In Array (One at a time)
// note the '/' character before the '[' 
//    to indicate that we are pulling from an array
for (i = 0; i < 10; i++) {
   var individualArrayElement = message.getNode('arrayExample/[' + (i+1) + ']');
   qie.debug(individualArrayElement);
}

The message now looks like:

{
   "arrayExample": [
      "ABCD0",
      "ABCD1",
      "ABCD2",
      "ABCD3",
      "ABCD4",
      "ABCD5",
      "ABCD6",
      "ABCD7",
      "ABCD8",
      "ABCD9"
   ]
}

The debug logs will show the whole arrayExample in the first log, and then each individual array element by itself.

In this example we only used a simple string in the array, but the array could be filled with more complex JSON objects as well.

by brandon-w-8204 (34.9k points)
0 votes
In QIE, JSON and XML arrays are referenced differently because JSON has explicit array syntax, while XML uses repeated elements selected with XPath predicates.

JSON: use JPath with / hierarchy plus an array qualifier.

Examples:

/phoneNumbers → the whole array

/phoneNumbers/[1] → first array item

/phoneNumbers/[usage="home"]/number → the number field from the matching array object

XML: use standard XPath. XML doesn’t have a native “array” type; what acts like an array is usually a set of repeating sibling elements.

Examples:

/bookstore/book → all repeated book elements

/bookstore/book[1] → first book

/bookstore/book[last()] → last book

A simple way to think about it:

JSON array item: /parentArray/[n]

XML repeated element: /parent/element[n]

QIE’s training docs summarize this as:

JSON JPath: {/ParentObject/ChildArray/[instance]/ChildProperty}

XML: standard XPath notation

So the practical difference is:

In JSON, the array itself is a node and you step into an item with /[n].

In XML, repeated elements are just matching nodes, and you select one with [n] directly on the element name.

Note: referencing an array  in JSON using the XML repeated element node path nomenclature will return incorrect results.
ago by gary-t-8719 (15.6k points)
edited ago by gary-t-8719
...