-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":"[email protected]",
      "id":"306",
      "updated":"2018-02-13 07:23:19",
      "website":"",
      "outdated":"danger",
      "contactid":"175"
   },
   {
      "artistname":"2Cellos",
      "publicistName":"Angela Barkan",
      "publicistEmail":"[email protected]",
      "id":"404",
      "updated":"2015-03-11 05:05:12",
      "website":"",
      "outdated":"danger",
      "contactid":"192"
   },
   {
      "artistname":"3 Doors Down",
      "publicistName":"Taylor Vaughn",
      "publicistEmail":"[email protected]",
      "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.

6
  • uhm. well, i mean, it's already an array. Most likely $json is null. How did you define it? Commented Oct 15, 2018 at 18:11
  • @KevinB $json = <<< JSON [ { "artistname":"1 Wild Night1", "publicistName":"Amy Sciarretto", "publicistEmail":"[email protected]", "id":"306", "updated":"2018-02-13 07:23:19", "website":"", "outdated":"danger", "contactid":"175" } ] JSON; Commented Oct 15, 2018 at 18:13
  • I need JSON to turn into an array. I have the data assigned to the json variable. Commented Oct 15, 2018 at 18:16
  • 1
    Possible duplicate of Detect bad json data in PHP json_decode()? because you need to use json_last_error() for more insight into your error. Commented Oct 15, 2018 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) {. Commented Oct 15, 2018 at 18:37

1 Answer 1

-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();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.