2

I have a JSON array with multiple objects and trying to use json_decode to make an associate array.

Sample data

$json='[{   
         type: "cool",
         category: "power",
         name: "Robert Downey Jr.",
         character: "Tony Stark / Iron Man",
         bio: "cool kid"
     },
       {
         type: "cool",
         category: "power",
         name: "Chris Hemsworth",
         character: "Thor",
         bio: "cool kid"
     },
     {
         type: "NotCool",
         category: "nothing",
         name: "Alexis Denisof",
         character: "The Other",
         bio: "cool kid"
     }]';

Here's what I am doing:

$data = json_decode($json, true);

which gives me a NULL result. What am I doing wrong?

(I'm new to PHP.)

3
  • i believe you need quotes around the property names as well Commented Aug 30, 2013 at 14:32
  • 2
    This isn't valid JSON. The keys in the objects need to be quoted, with double quotes ("). Such as: {"type": "NotCool",...}. Commented Aug 30, 2013 at 14:33
  • 2
    That is not valid JSON. It is a valid JS object initialization, but for JSON, the keys have to be enclosed by " (see http://json.org/). Commented Aug 30, 2013 at 14:34

4 Answers 4

2

Your JSON string is invalid: keys need to be quoted as well. Use JSONlint website, to check JSON validity.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly, but you should check this out Mchl: youtube.com/watch?v=13FV1GaA20I
Lol. I had probably started writing something else, then changed my mind. I'm not native to English, so I rarely do mix up you're and your ;)
1

Create Validate Json Try this

<?php
$json='[
    {
        "type": "cool",
        "category": "power",
        "name": "Robert Downey Jr.",
        "character": "Tony Stark / Iron Man",
        "bio": "cool kid"
    },
    {
        "type": "cool",
        "category": "power",
        "name": "Chris Hemsworth",
        "character": "Thor",
        "bio": "cool kid"
    },
    {
        "type": "NotCool",
        "category": "nothing",
        "name": "Alexis Denisof",
        "character": "The Other",
        "bio": "cool kid"
    }
]';
$data = json_decode($json, true);
echo "<pre>" ;
print_r($data);
?>

Comments

1

This isn't valid JSON. The keys in the objects need to be quoted, with double quotes (").

It should be:

$json='[{
     "type": "cool",
     "category": "power",
     "name": "Robert Downey Jr.",
     "character": "Tony Stark / Iron Man",
     "bio": "cool kid"
},
{
     "type": "cool",
     "category": "power",
     "name": "Chris Hemsworth",
     "character": "Thor",
     "bio": "cool kid"
},
{
     "type": "NotCool",
     "category": "nothing",
     "name": "Alexis Denisof",
     "character": "The Other",
     "bio": "cool kid"
}]';

Comments

1

you need double quotes around property names so it should be

JSON

[{   
    "type" : "cool",
    "category" : "power",
    "name" : "Robert Downey Jr.",
    "character" : "Tony Stark / Iron Man",
    "bio" : "cool kid"
}]

just try

PHP

echo json_encode(array("name" => "Tony Stark"));

and you will see valid json

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.