0

I'm trying to read a json file and parse it to an array, and then acces that array.

This is my JSON file:

{
    "stijn",
    "bert",
    "tom"
}

This is how I try to acces it and convert it to an array:

$string = file_get_contents(__DIR__."/first.json");
$array = json_decode($string, true);

if I do a var_dump of $array I get "null", if I do it of $string I get the contents of the JSON file.

I think that my JSON file is not properly formatted, but if I search for some examples they are not suitable for my list of names.

2
  • Try using square brackets for the JSON array. So [ 'stijn', ...]. Commented Nov 3, 2014 at 15:54
  • Good way to test your json is via jsonlint.com Commented Nov 3, 2014 at 16:12

2 Answers 2

2

you are right, your json is not formatted properly. you need to show that it is an array, so like this:

{
    names: [
        "stijn",
        "bert",
        "tom"
    ]
}

OR

[
    "stijn",
    "bert",
    "tom"
]

notice that square brackets are used for an array. if you use the curly braces, it's looking for a key/value pair

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

1 Comment

I will accept this answer within a few minutes, this one has worked and provided me with some extra helpfull information about json, thanks!
0

What about ?

[
    "stijn",
    "bert",
    "tom"
]

Comments

Your Answer

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