0

Sorry for the bad title, but I don't know how to create following JSON in PHP:

{ 
   "id":"1",
   "method":"getData",
   "params":{ 
      "options":{ 
         "element":{ 
            "id":"1a_ext",
            "type":1,
            "keyType":"externalkey"
         },
         "moreInfo":true,
         "userFields":[ 
            "id",
            "name",
            "longname",
            "externalkey"
         ]
      }
   },
   "jsonrpc":"2.0"
}

I don't know to do the part after "params" (how do I "put" options "into" params) - for the other parts I know what I have to do:

public static function getData(){
            $json = array(
                "id" => self::id(),
                "method" => "getData",
                "params" => array(
                    "id" => self::$userid,
                    "type" => self::$type
                ),
                "jsonrpc" => "2.0"
            );
            $json = json_encode($json, true);
            return self::request($json);
        }

I would really appreciate your help, thanks!

1
  • You can check result of json_decode($jsonString, true), for example. This will return array as you need and using this info you will create needed method Commented Sep 20, 2019 at 10:18

3 Answers 3

3

You directly can assign to the params keys like

$json['params']['options'] = $your_options;

Full version of your code as an example

public static function getData(){
    $json = array(
        "id" => self::id(),
        "method" => "getData",
        "params" => array(
            "id" => self::$userid,
            "type" => self::$type
        ),
        "jsonrpc" => "2.0"
    );

    # add something to param index
    $json['params']['options'] = $your_options;

    $json = json_encode($json, true);
    return self::request($json);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why are you passing true as a second param for json_encode?
for bitmasking during encoding, we have to pass true as an option.
The second param excepts an int (a flag) not a boolean value.
1

You can create the this in array format in PHP and then JSON encode:

$arr = [
    'id' => 1,
    'method' => 'getData',
    'params' => [
        'options' => [
            'element' => [
                'id' => '1a_ext',
                'type' => 1,
                'keyType' => 'externalKey'
            ],
            'moreInfo' => true,
            'userFields' => [
                'id',
                'name',
                'longname',
                'externalKey'
            ]
        ]
    ],
    'jsonrpc' => '2.0'
];

$json = json_encode($arr);

Comments

0

Instead of spoonfeeding, i would like to help related, Whenever if you find difficulties to create an array representation of a JSON then you should use var_export(array, true) the second parameter must be true to return the variable representation instead of outputting it

<?php
    $json_str = '{
      "id": "1",
      "method": "getData",
      "params": {
        "options": {
          "element": {"id": "1a_ext", "type": 1, "keyType": "externalkey"},
          "moreInfo": true,
          "userFields": ["id", "name", "longname", "externalkey"]
        }
      },
      "jsonrpc": "2.0"
    }';
    $json_arr = var_export(json_decode($json_str, true), true);
    print_r($json_arr);

check the output here https://paiza.io/projects/eUZZDsTsSFSM4m9WMl05Ow

$json_arr is an array representation for your JSON, now you can dynamic the array values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.