0
{"email":"[email protected]","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}

I'm having trouble parsing this string. It's from Sendgrid's Event API, it seems to be "almost" JSON, but json_decode won't work. My goal is to get the data into an array, then into a MySQL table. I'm not asking anyone to write the code for me, just point me toward the correct method. Do I use the explode function, then json_decode?. (I'm slowly teaching myself PHP, sorry if the question is not clear)

4
  • Why do you say "almost"? It seems valid according to JSONlint. Maybe there is some code or other details you might want to share? Commented Sep 1, 2012 at 15:09
  • 2
    It looks like JSON to me - have you tried plugging the result into some online json validator to be sure? Perhaps its a bug on their part if it is not returning proper json. Commented Sep 1, 2012 at 15:10
  • possible duplicate of PHP decoding json Commented Sep 1, 2012 at 15:10
  • Thanks for sharing JSONlint, I will go back to coding and see if I can figure it out, I was able to use json_decode successfully on a short json string but not the above (it gave me NULL), I will try again. Thanks again. Commented Sep 1, 2012 at 15:25

1 Answer 1

0

I think you're not using the json_decode function with $assoc = true, so you're getting an object rather than an array

$json = '{"email":"[email protected]","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}';

var_dump(json_decode($json, true));

You'll get the result as array;

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

Comments

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.