Friday, May 31, 2019

Encoding PHP array to JSON, how to encode square brackets [] and deep nested JSON

Deeply nested and squared brackets in JSON

To make square brackets in JSON, which is basically array in JSON you need to use the  array operator twice, nested. So make it look like this. write some PHP
array(array('something'....
$data = json_encode(array(
    'list'=>array(array('commerce_price_aud' => array (....
Here's another example of deeply nested JSON with square brackets.

   $options = array(
     'name' => 'MySystem',
     'state' => 'INVENTORY',
     'lifeCycleState' => 'INVENTORY',
     'type' => 'My system Type',
     'gateway'  => array('serialNumber' => '1234', 'creationDate' => '12345678' ),
     'subscription'  => array('identifier' => '1234', 'operator' => '12345678' ),
     'labels' => array('labege'),
     'applications' => array(array('uid' => '171b9f022cd14c9b8e92d9ba6764db81')),
     'metadata'  => array('name1' => 'vale1', 'name2' => 'vale2' ),
     'communication' => array('msci' => array('host' => null, 'user' => null, 'password' => null),
         'm3da' => array('registrationPassword' => null, 'password' => null),
         'rest' => array('password' => null))
   );

   $out = json_encode($options);
...
looks like this
{
   "name": "MyFirstSystem",
   "state": "INVENTORY",
   "lifeCycleState": "INVENTORY",
   "type" : "my system type",
   "gateway":    {
      "serialNumber": "88545687",
      "creationDate": "1337948436603"
   },
   "subscription" : {
        "identifier" : "4569874563254156984",
        "operator": "OPERATOR_NAME"
    },
   "labels": ["labege"],
   "applications": [   {
      "uid": "171b9f022cd14c9b8e000000000000"
   }],
   "metadata": {
      "name1": "value1",
      "name2": "value2"
   },
   "communication" :   {
      "msci" :    {
          "host" : null,
          "user" : null,
          "password" : null
      },
      "m3da" :    {
          "registrationPassword" : null,
          "password" : null
      },
      "rest" :    {
          "password" : null
      }
   }
}
or could look like this in var_dump:
string(522) "{
     "name":"SIM 89332401000000000000",
     "state":"INVENTORY",
     "lifeCycleState":"INVENTORY",
     "type":"My system Type",
      "gateway":{
             "serialNumber":"1234",
             "creationDate":"1559339446"},
     "subscription":{
           "identifier":"899721912000000000000",
           "operator":"Sierra Wireless"},
     "labels":["labege"],
         "applications":[{
            "uid":"10592ab7d15342200000000000000"
      }],
     "metadata": {
        "name1":"vale1",
        "name2":"vale2"},
     "communication":{
        "msci":{"host":null,"user":null,"password":null},
     "m3da":{"registrationPassword":null,"password":null},
     "rest":{"password":null}}}"

No comments:

Post a Comment