IBM Integration Bus, Version 9.0.0.8 Operating Systems: AIX, HP-Itanium, Linux, Solaris, Windows, z/OS

See information about the latest product version

Using PHP arrays with JSON

PHP arrays are associative maps, in which the key can be an integer or a string.

Integer keys can be allocated automatically in ascending sequential order, to work in the same way as a traditional integer indexed array. PHP supports an empty index operator ([]), which can be used on the left side of an assignment to add an item to the array, using an integer index with the value of the highest current index plus 1.

PHP associative arrays are modeled as JSON object structures, because JSON arrays are anonymous.

Values in a JSON logical tree can be set using any of the PHP native types, including arrays. The type of array determines the resulting JSON logical tree shape: When a multidimensional PHP array variable is assigned to a JSON domain tree path, these rules are applied to each dimension of the array variable, so that an appropriate JSON array or object is formed in the message tree to model each dimension of the PHP array.
When you create a JSON message tree in PHP using individual value assignments, you can create JSON array items by appending the target path with the PHP array append operator ([]) in the assignment. You can use the [] operator on a path in the following ways: If you try to create an array item using a target path with the [] operator, and the path resolves to an existing JSON object element in the tree, an exception is thrown.

The PHPCompute node supports the PHP constant MB_JSON_ARRAY, which you can use in the MbElement setType() method to change an existing JSON object element into a JSON array. This method is typically used when elements have been created by copying from a tree owned by another parser domain.

To access or change an existing value of a JSON array item in a tree, you can use an array index ([index]) to select the required item. JSON array items cannot be created in the JSON tree by using the [index] form; attempts to do so result in an exception being thrown.

You can also access non-array elements in the JSON domain tree, as if the tree were an associative array, by using an index key name. For example, to select an element called street, which is a child of an element called address, use the following path construct:
->address['street']

The following examples demonstrate how you can use PHP arrays with JSON:

Creating a JSON array message from individual PHP variables

You can create a JSON array and append items to it by using a target path with an array append index operator ([]):
    $strVar = "this";
    $boolVar = true;
    $output_assembly->JSON->Data[] = $strVar;
    $output_assembly->JSON->Data[] = $boolVar;

You must use the array append index operator ([]) to append to an array; you cannot use the [index] form.

The following broker message tree is produced:
 (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01001000:Array     ): Data = (  
        (0x03000000:NameValue): Item = 'this' (CHARACTER)
        (0x03000000:NameValue): Item = TRUE (BOOLEAN)
      )
  )
This message tree is serialized to the following JSON bit stream:
["this",true]
You can extend this example to build a multidimensional JSON array:
$output_assembly->JSON->Data[][] = '00';
$data = $output_assembly->JSON->Data;
$data[0][] = '01';
$data[][]  = '10';
$data[1][] = '11';
The resulting message tree is serialized to the following JSON bit stream:
[["00","01"],["10","11"]]

Creating a JSON array message from a PHP indexed array

You can create a PHP array with contiguous integer keys, and assign it to the message tree element that you want to form the JSON array:
    $arr = array("this", "that");
    $output_assembly->JSON->Data = $arr;
The following broker message tree is produced:
 (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01001000:Array     ): Data = (  
        (0x03000000:NameValue): 0 = 'this' (CHARACTER)
        (0x03000000:NameValue): 1 = 'that' (CHARACTER)
      )
  )
This message tree is serialized to the following JSON bit stream:
["this", "that" ] 

Creating a JSON object message from a PHP associative array

This example shows how to create a PHP array with named keys, and assign it to the message tree element that you want to form the JSON object:
    $arr = array('first' => "1st", 'second' => "2nd");
    $output_assembly->JSON->Data = $arr;
The following broker message tree is produced:
  (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01000000:Object  ): Data = (
        (0x03000000:NameValue): first = '1st' (CHARACTER)
        (0x03000000:NameValue): second = '2nd' (CHARACTER)
      )
  )
You can access the array elements using the key name in the array index:
$var1 = $output_assembly->JSON->Data['second']; // 2nd
$var2 = $output_assembly->JSON->Data['none'];   // null
This is serialized to the following JSON bit stream:
{"first":"1st","second":"2nd"} 

Creating a JSON message from a PHP integer index multidimensional array

You can assign the array to the element that is to hold the data. The top level and the nested level of the PHP array are integer indexed arrays, so both are created as JSON arrays, resulting in a multidimensional JSON array:
$arr = array(array("a1","a2"),array("b1","b2"));
$output_assembly->JSON->Data = $arr;
The following broker message tree is produced:
 (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01001000:Array     ): Data = (  
        (0x01001000:Array     ): 1 = (  
          (0x03000000:NameValue): 1 = 'a1' (CHARACTER)
          (0x03000000:NameValue): 2 = 'a2' (CHARACTER)
        )
        (0x01001000:Array     ): 2 = (  
          (0x03000000:NameValue): 1 = 'b1' (CHARACTER)
          (0x03000000:NameValue): 2 = 'b2' (CHARACTER)
        )
      )
  )
This message tree is serialized to the following JSON bit stream:
[["a1","a2"],["b1","b2"]]

Creating a JSON message from a PHP mixed associative and integer indexed multidimensional array

You can assign the array to the element that is to hold the data. The top level of the PHP array is associative, with string key names, and so becomes a JSON object. The nested level of the PHP array is using integer indexed arrays, and is created as a JSON array.
$arr = array("top1" => array("a1","a2"), "top2" => array("b1","b2"));
$output_assembly->JSON->Data = $arr;
The following broker message tree is produced from the example shown above:
 (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01000000:Object     ): Data = (
        (0x01001000:Array     ):top1 = ( 
          (0x03000000:NameValue): 1 = 'a1' (CHARACTER)
          (0x03000000:NameValue): 2 = 'a2' (CHARACTER)
        )
        (0x01001000:Array     ):top2 = ( 
          (0x03000000:NameValue): 1 = 'b1' (CHARACTER)
          (0x03000000:NameValue): 2 = 'b2' (CHARACTER)
        )
      )
  )
This message tree is serialized to the following JSON bit stream:
{"top1":["a1","a2"],"top2":["b1","b2"]}

Updating a JSON array in the logical tree

[["a1","a2"],["b1","b2"]]
The following broker message tree is produced from the JSON input shown above:
 (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01001000:Array     ): Data = (  
        (0x01001000:Array     ): Item = (  
          (0x03000000:NameValue): Item = 'a1' (CHARACTER)
          (0x03000000:NameValue): Item = 'a2' (CHARACTER)
        )
        (0x01001000:Array     ): Item = (  
          (0x03000000:NameValue): Item = 'b1' (CHARACTER)
          (0x03000000:NameValue): Item = 'b2' (CHARACTER)
        )
      )
  )
You can update this message with PHP, using [index] on the element that is holding the array to access existing elements, and the [] form to create additional elements:
output_assembly->JSON->Data[0][0] = "A1";
output_assembly->JSON->Data[1][1] = "B2";
output_assembly->JSON->Data[1][] = "New3";
This example produces the following JSON output:
[["A1","a2"],["b1","B2","new3"]]

Copying an XML domain message subtree with repeating elements to a JSON domain tree and converting it to a JSON array using PHP

You can use the PHP constant MB_JSON_ARRAY to explicitly test and set the Array type of a message tree element. For example, you can use MB_JSON_ARRAY if you are copying message tree data from an XML domain to a JSON domain, or from a JSON domain to an XML domain. The following XML input contains repeating elements:
<doc>
      <cats>
        <cat>thing1</cat>
        <cat>thing2</cat>
      </cats>
</doc>
The following broker message tree is produced by the XMLNSC parser:
(0x01000000:Folder):XMLNSC     = ( ['xmlnsc' : 0xhhhhhh]
      (0x01001000:Folder     ): doc = (
        (0x01001000:Folder     ): cats = (
          (0x03000000:NameValue): cat = 'thing1' (CHARACTER)
          (0x03000000:NameValue): cat = 'thing2' (CHARACTER)
        )
      )
 )
The message flow can convert this broker message tree into a JSON message, to be serialized in the following way:
{"cats":["thing1","thing2"]}
You can transform the message using PHP, as shown in the following example:
$output_assembly->JSON->Data = $input_assembly->XMLNSC->doc;
$output_assembly->JSON->Data->cats->setType(MB_JSON_ARRAY);
This transformation produces the following message tree, which serializes to JSON format:
(Message):JSON     = ( ['json' : 0xhhhhhh]
    (0x01000000:Obejct):Data     = (
        (0x01001000:Array     ): cats = (
          (0x03000000:NameValue): cat = 'thing1' (CHARACTER)
          (0x03000000:NameValue): cat = 'thing2' (CHARACTER)
        )
     )
 )

Working with a JSON object with a repeating name

The JSON specification states that JSON objects should not have duplicate names. However, it is possible for a message to contain this format; for example, in the following JSON message the name cat is repeated:
{"cat":"thing1","cat":"thing2" }
The JSON parser produces the following broker message tree from the JSON input shown above:
 (Message):JSON     = ( ['json' : 0xhhhhhh]
      (0x01001000:Object     ): Data = (
        (0x03000000:NameValue): cat = 'thing1' (CHARACTER)
        (0x03000000:NameValue): cat = 'thing2' (CHARACTER)
      )
  )

This is an invalid PHP data structure because it has a duplicate key, which cannot exist in an associative array. As a result, this form of JSON data cannot be accessed, created, or modified from a PHPCompute node. To process this type of JSON data, the message flow must use either ESQL or Java™.


bc28414_.htm | Last updated Friday, 21 July 2017