Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert json to array php gives Array to string conversion

Tags:

json

php

I have a json as shown below.I want to access the value of adult.However when i do echo json_decode($json_response, true); i get Array to string conversion.What is wrong here ?

 {
      "responses": [
        {
          "safeSearchAnnotation": {
            "adult": "VERY_UNLIKELY",
            "spoof": "VERY_UNLIKELY",
            "medical": "UNLIKELY",
            "violence": "LIKELY"
          }
        }
      ]
    }
like image 431
user2650277 Avatar asked Oct 26 '16 15:10

user2650277


People also ask

Can we convert JSON array to string?

Stringify a JavaScript ArrayUse the JavaScript function JSON.stringify() to convert it into a string.

What does json_decode return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

What is json_encode and json_decode in PHP?

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.


1 Answers

The function json_decode returns an array. You can't echo an array, or you'll get that conversion error.

You want to use print_r instead:

print_r(json_decode($json_response, true));

See here: https://3v4l.org/K3UfP

like image 188
jszobody Avatar answered Nov 03 '22 00:11

jszobody