Malformed JSON body when importing data to dataset by using API


#1

When I run the PHP-code below the response of the API is:
Xibo\OAuth2\Client\Exception\XiboApiException: {
“error”: {
“message”: “Malformed JSON body, rows and uniqueKeys are required”,
“code”: 422,
“data”: []
}


PHP-CODE

$provider = new \Xibo\OAuth2\Client\Provider\Xibo([
‘clientId’ => // The client ID assigned to you by the provider
’clientSecret’ => // The client password assigned to you by the provider
’redirectUri’ => ‘’,
‘baseUrl’ =>
]);

$xiboClient = new \Xibo\OAuth2\Client\Provider\XiboEntityProvider($provider);

$json = ‘{“uniqueKeys”:[“Col1”],“rows”:[{“Col1”: “value1”}]}’;
$jsonParam = json_decode ($json);

$result = $xiboClient->post(’/dataset/importjson/2’, $jsonParam);

The description of the post-function can be found on the following link (line 84):

As you can see the second parameter should be an array. For that i use json_decode ($json); but the API doesn’t accept this format (“Malformed JSON body, rows and uniqueKeys are required”).

Do you have an idea what’s going wrong? Thanks a lot!


#3

That call expects JSON in the request body, which you can achieve using your original code and providing the json property to the EntityProvider.

$json = '{"uniqueKeys":["Col1"],"rows":[{"Col1": "value1"}]}';
$jsonParam = json_decode ($json);
$result = $xiboClient->post('/dataset/importjson/2', ['json' => $jsonParam]);

Alternatively, you can avoid constructing the string yourself, and use an array from the outset, like:

$result = $xiboClient->post('/dataset/importjson/2', ['json' => [
  'uniqueKeys' => ['Col1'],
  'rows' => [
    ['Col1' => 'value1']
  ]
]]);

#4

Thanks al lot!! [‘json’ => $jsonParam] instead of $jsonParam solves the problem