-1

I'm trying to use the advice given in How to search through a JSON Array in PHP to lookup country names based on a country code in a .json file.

I have a file, countrycodes.js which is formatted like this:

countries = [
    {code: "GB", name: "United Kingdom"},
    {code: "AF", name: "Afghanistan"},
    // ...
    {code: "ZM", name: "Zambia"},
    {code: "ZW", name: "Zimbabwe"}
];

The variable, countries has to be there because part of the application relies on this.

In PHP I've done the following:

$str = file_get_contents('countrycodes.js');
var_dump($str);

This outputs a string:

string(9642) "countries = [
{code: "GB", name: "United Kingdom"},
{code: "AF", name: "Afghanistan"},
// ...

However, when I try and json_decode it, the following gives NULL:

$str = file_get_contents('countrycodes.js');
$json = json_decode($str);
var_dump($json);

I don't know why this is because json_decode accepts a string, and this is what's given on the link I posted above? I tried removing the JavaScript variable (countries = ) but this made no difference.

Ultimately what I want to do is be able to give PHP a country code such as 'GB' and get it to return the appropriate name e.g. 'United Kingdom'. My understanding of this is the json_decode part will need to work before this is possible.

For reference, the reason countries = was being used is for populating a <select> element based on advice given here: Populating select using ajax json array

12
  • 1
    countrycodes.js is Javascript, not JSON... Commented Aug 22, 2017 at 9:02
  • Ok thanks. Are there any ways to convert it, without having to store it in 2 separate files? Commented Aug 22, 2017 at 9:03
  • 1
    No way that is sane. Commented Aug 22, 2017 at 9:04
  • 2
    @RahulMeshram Really? It's not JSON :P Commented Aug 22, 2017 at 9:05
  • 1
    @RahulMeshram Huh? Passing true to json_decode isn't going to help if the input isn't valid JSON. Commented Aug 22, 2017 at 9:08

2 Answers 2

4

If you json_decode returns null, this means the input string is not in a valid format. Your file contains a javascript variable and not a JSON string.

This is how the string should probably formatted:

{
    "countries": [
        {"code": "GB", "name": "United Kingdom"},
        {"code": "AF", "name": "Afghanistan"},
        {"code": "ZM", "name": "Zambia"},
        {"code": "ZW", "name": "Zimbabwe"}
    ]
}
Sign up to request clarification or add additional context in comments.

5 Comments

I can reformat the file so it looks like that. However this will inevitably break what I'd had a hard time implementing to populate a select element based on the file contents - details are here stackoverflow.com/questions/45795518/…. So is it possible to populate a select based on the format you've given? I'm trying to think of a solution which will cover both requirements. Thanks.
That is not a very good solution in javascript. You should look at the $.getJSON() function to get the json file at the client side.
@Jerodev can you confirm that that's how the string should be formatted? Because it's still giving NULL as the output when json_decode()-ing that.
This is most certainly a valid json string. Please update your answer with your new string so we can see what the problem is.
My fault. When I'd enclosed code and name in quotes I'd put the colon inside the string! It's fixed now. Thanks for your patience/help.
0

Extending on Jerodev's answer, if you cannot will not change the representation, you could do the following.

Modify your file so that you wrap the keys with quotes. Something like,

countries = [
    {"code": "GB", "name": "United Kingdom"},
    {"code": "AF", "name": "Afghanistan"},
    // ...
    {"code": "ZM", "name": "Zambia"},
    {"code": "ZW", "name": "Zimbabwe"}
];

And then just decode the json part.

$str = file_get_contents('countrycodes.js');
$str = substr($str,12, strlen($str)-13); // do not include the semicolon
var_dump(json_decode($str));

However this is not a good approach, so I'd still suggest to look for other ways to populate the data in your element.

Note that 12 is the position where [ starts, if you have made any changes to variable names or spaces, that (and 13) needs to change.

2 Comments

Thanks. I'm more than happy to reformat the file as per Jerodev's suggestion. This problem has originated from advice I was given in stackoverflow.com/questions/45795518/… from someone who had a 9k reputation, and their solution seemed to work for that particular problem. I'm going to reformat my file and look into $.getJSON() as suggested by @Jerodev.
Cool. Good choice!

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.