Home > database >  Unable to json decode string from json file
Unable to json decode string from json file

Time:09-09

I am facing an issue where I have a JSON array of objects in a .json file. I can get the content of the file using file_get_contents $str = file_get_contents($jsonFile); However when I perform json_decode on the content I just get null as result. Below is some of the content from the .json file

[
{
    "accreditation": false,
    "category.en": "Administration and Management",
    "category.fr": "Administration et gestion",
    "clientele.en": null,
    "clientele.fr": null,
    "courseid": 11749,
    "duree": "",
    "dureeminutes": 0,
    "establishmentaltname": "06-ciusss-cusm",
    "establishmentfullname": "Centre universitaire de santé McGill",
    "fcpresponsable": "",
    "idnumber": "",
    "idnumberalt": "",
    "imgurl": null,
    "ispartageable": false,
    "keywords": null,
    "lastupdate": 0,
    "modalite.en": "In Person",
    "modalite.fr": "En présentiel",
    "nombreinscriptions": 1,
    "parentestablishmentfullname": "Territoire CUSM",
    "parentestablishmentshortname": "CUSM-FCP",
    "partageable": "Locale",
    "shortname.en": "Formation Context 04072022 12h41",
    "shortname.fr": "Formation Context 04072022 12h41",
    "summary.en": "",
    "summary.fr": "",
    "title.en": "Formation Context 04072022 12h41",
    "title.fr": "Formation Context 04072022 12h41",
    "visible": false
},
{
    "accreditation": false,
    "category.en": "Administration and Management",
    "category.fr": "Administration et gestion",
    "clientele.en": null,
    "clientele.fr": null,
    "courseid": 11748,
    "duree": "",
    "dureeminutes": 0,
    "establishmentaltname": "06-ciusss-cusm",
    "establishmentfullname": "Centre universitaire de santé McGill",
    "fcpresponsable": "",
    "idnumber": "",
    "idnumberalt": "",
    "imgurl": null,
    "ispartageable": false,
    "keywords": null,
    "lastupdate": 0,
    "modalite.en": "In Person",
    "modalite.fr": "En présentiel",
    "nombreinscriptions": 1,
    "parentestablishmentfullname": "Territoire CUSM",
    "parentestablishmentshortname": "CUSM-FCP",
    "partageable": "Locale",
    "shortname.en": "Formation Contexte 040722 08h51m",
    "shortname.fr": "Formation Contexte 040722 08h51m",
    "summary.en": "",
    "summary.fr": "",
    "title.en": "Formation Contexte 040722 08h51m",
    "title.fr": "Formation Contexte 040722 08h51m",
    "visible": true
},
{
    "accreditation": false,
    "category.en": "Administration and Management",
    "category.fr": "Administration et gestion",
    "clientele.en": null,
    "clientele.fr": null,
    "courseid": 11747,
    "duree": "",
    "dureeminutes": 0,
    "establishmentaltname": "06-ciusss-cusm",
    "establishmentfullname": "Centre universitaire de santé McGill",
    "fcpresponsable": "",
    "idnumber": "",
    "idnumberalt": "",
    "imgurl": null,
    "ispartageable": false,
    "keywords": null,
    "lastupdate": 0,
    "modalite.en": "In Person",
    "modalite.fr": "En présentiel",
    "nombreinscriptions": 1,
    "parentestablishmentfullname": "Territoire CUSM",
    "parentestablishmentshortname": "CUSM-FCP",
    "partageable": "Locale",
    "shortname.en": "Formation Contexte 04072022",
    "shortname.fr": "Formation Contexte 04072022",
    "summary.en": "",
    "summary.fr": "",
    "title.en": "Formation Contexte 04072022",
    "title.fr": "Formation Contexte 04072022",
    "visible": false
}]

How can I convert it into valid json string for php or an array. The JSON is a valid JSON but after I use file_get_contents it inserts line breaks and \n like here: https://3v4l.org/5Zd7O Below is a snippet of my code:

$str = file_get_contents('jsondump.json');
var_dump(gettype($str));
var_dump($str);

$jsonArr = json_decode($str,1); // decode the JSON into an associative array
var_dump($jsonArr) ;
echo json_last_error_msg();

CodePudding user response:

  1. The JSON text you posted is OK. Unfortunately, that's NOT the text you're passing to json_decode(). Hence the error.

  2. Assuming your original .json file is OK, it appears that file_get_contents() is corrupting the JSON text.

  3. SUGGESTION:

http://truelogic.org/wordpress/2018/08/19/php-file_get_contents-for-utf-encoded-content/

One of the problems of file_get_contents() is that it messes up the data if the file contains special characters outside the standard ASCII character set.

The solution is to convert the encoding of the contents to UTF-8, but only after it has detected the desired encoding. So for instance if we know the file contains European languages like Spanish or French then we specify the detection for ISO-8859-1. For Arabic it would be ISO-8859-6 and so on.

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

CodePudding user response:

I did try with the JSON data you provided and it's working fine. You can check if you are using correct path where your JSON file is stored in file_get_contents().

Below is the example: https://3v4l.org/bFS59

  • Related