0

I'm trying to get content from a JSON file to put it in MySQL database using PHP, here is how I've been trying so far.

This is the JSON file AK.json:

[
{
    "CompanyName": "Logo Shirts Direct",
    "StreetAddress": "1001 Commerce Parkway South Dr Suite E",
    "Region": "Greenwood",
    "State": "IN",
    "PostCode": "46143",
    "Phone": "(888) 341-5646"
},
{
    "CompanyName": "L.F. GRAPHICS LLC",
    "StreetAddress": "Paterson, ",
    "Region": "Paterson",
    "State": "NJ",
    "PostCode": "07524",
    "Phone": "(973) 240-7033"
},
{
    "CompanyName": "Pacific Sportswear And Emblem Company",
    "StreetAddress": "San Diego, ",
    "Region": "Diego",
    "State": "CA",
    "PostCode": "92120",
    "Phone": "(619) 281-6688"
}
]

and in PHP:

<?php
$filename = 'AK.json';
$content = file_get_contents($filename);
print_r(json_decode($content,true));
?>

When I execute this script nothing happens, also I tried the gettype() function to get the type of the variable it returns NUll.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Talib Allauddin
  • 123
  • 3
  • 16

3 Answers3

2

It seems like your AK.json having a BOM. You can test it with

$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 === strncmp($content, $bom, 3)) {
    echo "BOM detected - file is UTF-8\n";
    $str = substr($content, 3);
}

Here is how you get rid of BOM and What is BOM?


Source: PHP Snippet

Community
  • 1
  • 1
Gabscap
  • 287
  • 4
  • 11
  • it returns `string(214) "[ { "CompanyName": "Logo Shirts Direct", "StreetAddress": "1001 Commerce Parkway South Dr Suite E", "Region": "Greenwood", "State": "IN", "PostCode": "46143", "Phone": "(888) 341-5646" } ]"` – Talib Allauddin Jul 22 '15 at 09:14
  • is there any problem with my PHP version or some extension? – Talib Allauddin Jul 22 '15 at 09:18
  • I think you have a BOM in your AK.json. You can test it with `$bom = pack("CCC", 0xef, 0xbb, 0xbf); if (0 === strncmp($content, $bom, 3)) { echo "BOM detected - file is UTF-8\n"; $str = substr($content, 3); }` – Gabscap Jul 22 '15 at 09:21
  • BOM detected - file is UTF-8, What is this? and how to eliminate this? – Talib Allauddin Jul 22 '15 at 09:23
  • [How to get rid of BOM](http://stackoverflow.com/questions/295472/how-do-i-remove-the-bom-character-from-my-xml-file) and [What is BOM?](http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom) – Gabscap Jul 22 '15 at 09:25
0

can you do :

$filename = './AK.json';
$content = file_get_contents($filename);
if($content === false){ 
  echo 'something wrong here';
}else{
  print_r(json_decode($content,true));
}

I think the problem is the path to your ressource AK.json, be sure he is in the right place :)

MaximeK
  • 2,039
  • 1
  • 10
  • 16
  • the given path is just fine, because when I echo the content without `json_decode` function it prints out the string data. but when I apply the `json_decode` it becomes NULL – Talib Allauddin Jul 22 '15 at 09:21
  • so like @Gabscap said it might be a BOM, to remove it go [here](http://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before-doctype) – MaximeK Jul 22 '15 at 09:25
0

try this

<?php
$data='
[
{
    "CompanyName": "Logo Shirts Direct",
    "StreetAddress": "1001 Commerce Parkway South Dr Suite E",
    "Region": "Greenwood",
    "State": "IN",
    "PostCode": "46143",
    "Phone": "(888) 341-5646"
},
{
    "CompanyName": "L.F. GRAPHICS LLC",
    "StreetAddress": "Paterson, ",
    "Region": "Paterson",
    "State": "NJ",
    "PostCode": "07524",
    "Phone": "(973) 240-7033"
},
{
    "CompanyName": "Pacific Sportswear And Emblem Company",
    "StreetAddress": "San Diego, ",
    "Region": "Diego",
    "State": "CA",
    "PostCode": "92120",
    "Phone": "(619) 281-6688"
}
]';
echo '<pre>';
print_r(json_decode($data,true));
echo '</pre>';

if its working then your AK.json file is contain any character outside of []