0

I have a file test.json like that:

{
    {"code":"ab","name":"Abkhaz","nativeName":"аҧсуа"},
    {"code":"aa","name":"Afar","nativeName":"Afaraf"},
    {"code":"af","name":"Afrikaans","nativeName":"Afrikaans"},
    {"code":"ak","name":"Akan","nativeName":"Akan"},
    {"code":"sq","name":"Albanian","nativeName":"Shqip"},
    {"code":"am","name":"Amharic","nativeName":"አማርኛ"}
}

I need to convert in an array in php.

For that I have tried this but don't work:

$string = file_get_contents("test.json");
$json_array = json_decode($string, true);

print_r($json_array); //return empty
echo count($json_array); //return 0
2
  • 1
    Your JSON is not validating at jsonlint.com Commented May 12, 2017 at 20:30
  • 2
    You've got an array, not a hash -- the opening { and closing } should be [ and ]. Commented May 12, 2017 at 20:40

1 Answer 1

1

This is the valid JSON (use http://pro.jsonlint.com/ to check your JSON strings, you need a start and end bracket [] instead of curly braces {}):

[
    {
        "code": "ab",
        "name": "Abkhaz",
        "nativeName": "аҧсуа"
    },
    {
        "code": "aa",
        "name": "Afar",
        "nativeName": "Afaraf"
    },
    {
        "code": "af",
        "name": "Afrikaans",
        "nativeName": "Afrikaans"
    },
    {
        "code": "ak",
        "name": "Akan",
        "nativeName": "Akan"
    },
    {
        "code": "sq",
        "name": "Albanian",
        "nativeName": "Shqip"
    },
    {
        "code": "am",
        "name": "Amharic",
        "nativeName": "አማርኛ"
    }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.