-4

before you mark this as a duplicate, I've tried probably every relevant question on StackOverflow and nothing seems to be working.

I have a very large JSON file (8112 lines) that I'm trying to convert to an array and then enter the data into a database. So far, I can't even get the JSON to turn into an array.

file.json

[
   {
      "artistname":"1 Wild Night1",
      "publicistName":"Amy Sciarretto",
      "publicistEmail":"amy@amysciarretto.com",
      "id":"306",
      "updated":"2018-02-13 07:23:19",
      "website":"",
      "outdated":"danger",
      "contactid":"175"
   },
   {
      "artistname":"2Cellos",
      "publicistName":"Angela Barkan",
      "publicistEmail":"angela.barkan@sonymusic.com",
      "id":"404",
      "updated":"2015-03-11 05:05:12",
      "website":"",
      "outdated":"danger",
      "contactid":"192"
   },
   {
      "artistname":"3 Doors Down",
      "publicistName":"Taylor Vaughn",
      "publicistEmail":"Taylor.Vaughn@umusic.com",
      "id":"760",
      "updated":"2016-03-04 09:32:06",
      "website":"",
      "outdated":"danger",
      "contactid":"205"
   },

Here are three entries, but there's a bunch more below that. I'm not entirely sure how many there are.

file.php

$arr = json_decode($json, true);

var_dump($arr); // response is NULL

if (is_array($json)) {
foreach($json as $data) {
  echo $data['artistname'];
  echo $data['publicistName'];
  echo $data['publicistEmail'];
}
} else {
  echo "not an array"; // this is the response for the if statement
}

What am I doing wrong?

Also, I've tested the entire JSON file on JSONLint and one of the other services, its perfectly valid according to them.

CodyLeeK
  • 19
  • 1
  • 5
  • uhm. well, i mean, it's already an array. Most likely `$json` is null. How did you define it? – Kevin B Oct 15 '18 at 18:11
  • @KevinB `$json = <<< JSON [ { "artistname":"1 Wild Night1", "publicistName":"Amy Sciarretto", "publicistEmail":"amy@amysciarretto.com", "id":"306", "updated":"2018-02-13 07:23:19", "website":"", "outdated":"danger", "contactid":"175" } ] JSON; ` – CodyLeeK Oct 15 '18 at 18:13
  • I need JSON to turn into an array. I have the data assigned to the json variable. – CodyLeeK Oct 15 '18 at 18:16
  • Without seeing the heredoc as true code, I can see you put a SPACE inside << – Forbs Oct 15 '18 at 18:30
  • 1
    Possible duplicate of [Detect bad json data in PHP json\_decode()?](https://stackoverflow.com/questions/2348152/detect-bad-json-data-in-php-json-decode) because you need to use `json_last_error()` for more insight into your error. – MonkeyZeus Oct 15 '18 at 18:36
  • 1
    Couple of things about the code - `if (is_array($json))` will always fail as `$json` is the original JSON string. This is also what you try and use in your next line `foreach($json as $data) {`. – Nigel Ren Oct 15 '18 at 18:37

1 Answers1

-1

Sigh. Got it to work. Here's the working code if anyone needs it. Thanks everyone.

$json = file_get_contents('publicists.json');
$data = json_decode($json);
foreach ( $data as $pub ) {
    $artist = $pub->artistname;
    $publicist = $pub->publicistName;
    $email = $pub->publicistEmail;
    $updated = $pub->updated;

    try
        {
      $sqlInsert = "INSERT INTO publicists(artist, publicist, email, updated)
    VALUES (:artist, :publicist, :email, :updated)";

      //use PDO prepared to sanitize data
      $statement = $db->prepare($sqlInsert);

      //add the data into the database
      $statement->execute(array(':artist' => $artist, ':publicist' => $publicist, ':email' => $email, ':updated' => $updated));

  }catch (PDOException $ex){
    $result = $ex->getMessage();
  }
}
CodyLeeK
  • 19
  • 1
  • 5