-3

I make an API call and try to convert the json Response to a php Array. However when checking with the is_array function, it turns out it isn't an Array.

Call to Api

$ch = curl_init("https://api.url.com/value/value");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , "token"));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);

Json the API call Returns:

[
  {
    "number":"65",
    "Field":"test",
    "Name":"test",
    "type":"Numeric",
    "MaximumLength":128,
    "MinimumLength":0,
    "Options":"required"
  }
]

and so on.

I decode it using

json_decode($result);

However, checking like this

if (is_array($result)) {
  echo "is array";
} else { 
  echo "is not an array!";
}

echoes "is not an Array".

I checked the json Response and it's valid json code. I also tried

json_decode($result, true);

with the same result.

Am I making some obvious mistake?

zoefrankie
  • 29
  • 1
  • 7

2 Answers2

4

The following code snippet seems to behave as expected (echoes 1) so your JSON is valid and will work correctly.

$result = '[{"ConditionCode":"1","Field":"test","Name":"test","FieldType":"Numeric","MaximumLength":128,"MinimumLength":0,"Options":"required"}]';

$x = json_decode($result, true);

echo($x[0]["ConditionCode"]);

I'm guessing you just ran json_decode on $result? json_decode doesn't set the value of the variable you feed it to the json decoded array. It simply returns the array so you have to assign this value to another variable (or itself in this case)

Try

$result = json_decode($result, true);

Instead of,

json_decode($result, true);
Dheeraj
  • 105
  • 1
  • 11
  • Is $result a string before you pass it in to the decode function? – Dheeraj May 17 '18 at 11:35
  • I do assign it back. I tried with both, $result = json_decode($result, true); and $result = json_decode($result); (without (,true). Yet i get the same result. – zoefrankie May 17 '18 at 11:38
  • Does echoing $result give you exactly what you expect? – Dheeraj May 17 '18 at 11:45
  • Try echoing gettype ( $result ) to check its type too. Try it on $result before running the decode as well as after. – Dheeraj May 17 '18 at 11:46
  • echoing $result Returns `1`, while gettype Returns `boolean` – zoefrankie May 17 '18 at 11:50
  • Something is off.. your $result variable isn't being assigned the string it should be.. Can you update the question with the rest of your code? – Dheeraj May 17 '18 at 12:46
  • Thanks to the help of deceze♦, i found the mistake. I was not setting curl to return a Response. Rather than return it, it just printed it. I fixed this by adding this line of code `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);`. Nonetheless, thanks for the help. – zoefrankie May 17 '18 at 14:04
  • Awesome, Glad you found the solution! Now maybe write a quick answer and accept it so any future visitor gets the solution to this question? – Dheeraj May 17 '18 at 14:35
  • Yeah i will do that. Edit: Actually i think i can't, since the question is marked as duplicate. – zoefrankie May 18 '18 at 07:01
0

You can use json_last_error() and json_last_error_msg() to see what was the problem with the json you tried to parse. I usually use GuzzleHttp which comes with the following wrapper for json_decode that throws an exception when decoding fails:

/**
 * Wrapper for json_decode that throws when an error occurs.
 *
 * @param string $json    JSON data to parse
 * @param bool $assoc     When true, returned objects will be converted
 *                        into associative arrays.
 * @param int    $depth   User specified recursion depth.
 * @param int    $options Bitmask of JSON decode options.
 *
 * @return mixed
 * @throws \InvalidArgumentException if the JSON cannot be decoded.
 * @link http://www.php.net/manual/en/function.json-decode.php
 */
function json_decode($json, $assoc = false, $depth = 512, $options = 0)
{
    $data = \json_decode($json, $assoc, $depth, $options);
    if (JSON_ERROR_NONE !== json_last_error()) {
        throw new \InvalidArgumentException(
            'json_decode error: ' . json_last_error_msg());
    }

    return $data;
}
solarc
  • 5,638
  • 2
  • 40
  • 51