0

I have a javascript file which includes the data in the following format.

    index = {
  "me" : {
    "id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
    "firstName" : "A=",
    "lastName" : "T",
    "pictureName" : "66s2c.jpg",
    "username" : "a-t"
  },
  "spaces" : [ {
    "org" : {
      "id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
      "firstName" : "An",
      "lastName" : "Tuli",
      "pictureName" : "66s2c.jpg",
      "username" : "arli"
    }
  }, {
    "user" : {
      "id" : "60c4a171-172f-4f66-9014-8b4cf3e476e6",
      "firstName" : "Ban",
      "lastName" : "Idris",
      "pictureName" : "../../../../default-pic/butterfly_200.png",
      "username" : "banun-idris"
    }
  } ]
}
users["524bd089-2a7b-4a71-ba23-16354d6351ae"] = {
  "id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
  "firstName" : "A",
  "lastName" : "T",
  "pictureName" : "66s2c.jpg",
  "username" : "a",
  "libraries" : [ "lEy27AZavfSR", "l0yApAoo2l4b", "lJl22YOtacxY", "l0UhMCvrMmka", "lJMWpIoFnaK4", "lCZ9cYYjVJcv", "l8kynpyoaej7" ]
}
 libraries["lEy27AZavfSR"] = {
  "id" : "lEy27AZavfSR",
  "name" : "My Main Library",
  "description" : null,
  "numKeeps" : 0,
  "keeps" : [ ]
}

keeps["k4r5UIugqgfk"] = {
  "id" : "k4r5UIugqgfk",
  "keptAt" : 1449613295000,
  "lastActivityAt" : 1449613295000,
  "title" : "",
  "url" : "",
  "note" : null,
  "tags" : [ ],
  "libraries" : [ "lJl22YOtacxY" ],
  "summary" : "",
  "messages" : [ ]
}

I need to import data from this type of format into my mysql database.

I can't finalize and nail down the approach to do this

I tried to get the file content into a php file and then convert them into a array string but i get a null result.

$jsondata = file_get_contents('trial_data.js');
$data = json_decode($jsondata, true);
var_dump($data);

The second approach I think i can take is load the data in jquery file and send it using AJAX to a php file which adds it to the database.

Which approach do you think i should take and how to go about it.

Plus i need to figure out the ID from the first index array, so that I can get the ID from the me and org object for which i need to get the rest of the information

10
  • depends how much data there is. What's shown could be manually turned into json and easily read by your php in about 2 minutes. If there is a lot it wouldn't take long (minutes not hours) to load it into html page and ajax it to php Commented Jul 13, 2016 at 17:58
  • Did you encode the array in javascript using JSON.stringify()? Commented Jul 13, 2016 at 17:58
  • @charlietfl my problem is not the time, but the issue is the $data in PHP is returning NULL, do I need to do some functions on it to convert into a PHP array Commented Jul 13, 2016 at 18:25
  • that isn't json currently is why php sees null when you try to decode it Commented Jul 13, 2016 at 18:26
  • @borna, i am getting the data from a export file, to do JSON.stringify() on it, i will have to take the second approach i think. load in jquery, do json.stringify and then pass the data to PHP using ajax Commented Jul 13, 2016 at 18:26

1 Answer 1

1

Since your data is not a valid JSON, you can use a regex to parse it, like this:

$file_content = file_get_contents('trial_data.js');
preg_match_all("/(\w+)\[\"([^\]]*)\"\]\s*\=\s*\{([^\}]*)\}/", $file_content, $matches);
foreach ($matches[1] as $key => $value) {
    $data[$value][$matches[2][$key]] = json_decode('{'.$matches[3][$key].'}');
}
print_r($data);
//For example:
echo $data['users']['524bd089-2a7b-4a71-ba23-16354d6351ae']->pictureName;
//Output: 66s2c.jpg

Demo: https://3v4l.org/fW5gH

Sign up to request clarification or add additional context in comments.

3 Comments

I have another problem, i also need to get the information for the required user from the top of the file it has the following format || index = { "me" : { "id" : "524bd089-2a7b-4a71-ba23-1635451ae", "firstName" : "a", "lastName" : "T", "pictureName" : "66s2c.jpg", "username" : "a-y" }, { "user" : { "id" : "60c4a171-172f-4f66-9014-8b4cf3e476e6", "firstName" : "B", "lastName" : "I", "pictureName" : "", "username" : "bi" } } ] } I need the id from the me field can you help me figure out the regex for the same.
Could you please add it to your question.
Hey did you check the change i made to the code, how do i get the id value from the me field in in index

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.