1

I am getting some json response at a frequent interval, I am storing the json response in a file. I want to decode it

[{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"processed","category":"cat facts","sg_event_id":"QdphcK0Jre4Q7L9Huwm_ug==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"deferred","category":"cat facts","sg_event_id":"vbfCZfSBz32ySl7j5nSayw==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"400 try again later","attempt":"5"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"delivered","category":"cat facts","sg_event_id":"tmFRu_j-NWZ4fZU4zRhDYg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"250 OK"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"open","category":"cat facts","sg_event_id":"LlbAt3ZNgC3yUoTt0ImdXg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"click","category":"cat facts","sg_event_id":"7seySmsaB5gncIjv4dmfGg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"bounce","category":"cat facts","sg_event_id":"xOeVKsSD2pcarTPw6r6q5g==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"500 unknown recipient","status":"5.0.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"dropped","category":"cat facts","sg_event_id":"-mk0ZOl1WgDTEteRC2olOw==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"Bounced Address","status":"5.0.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"spamreport","category":"cat facts","sg_event_id":"x-S6eSyCAzeEZoTrJvf0rg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"unsubscribe","category":"cat facts","sg_event_id":"RQ9N3MW13w8AWeGU_fwD7Q==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_unsubscribe","category":"cat facts","sg_event_id":"jIDVkh2-1yXXIJBOK-lVmg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_resubscribe","category":"cat facts","sg_event_id":"BtasI8e0rTH1GyCQHYX-Ag==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10}]

 [{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"processed","category":"cat facts","sg_event_id":"QdphcK0Jre4Q7L9Huwm_ug==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"deferred","category":"cat facts","sg_event_id":"vbfCZfSBz32ySl7j5nSayw==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"400 try again later","attempt":"5"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"delivered","category":"cat facts","sg_event_id":"tmFRu_j-NWZ4fZU4zRhDYg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"250 OK"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"open","category":"cat facts","sg_event_id":"LlbAt3ZNgC3yUoTt0ImdXg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"click","category":"cat facts","sg_event_id":"7seySmsaB5gncIjv4dmfGg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"bounce","category":"cat facts","sg_event_id":"xOeVKsSD2pcarTPw6r6q5g==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"500 unknown recipient","status":"5.0.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"dropped","category":"cat facts","sg_event_id":"-mk0ZOl1WgDTEteRC2olOw==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"Bounced Address","status":"5.0.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"spamreport","category":"cat facts","sg_event_id":"x-S6eSyCAzeEZoTrJvf0rg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"unsubscribe","category":"cat facts","sg_event_id":"RQ9N3MW13w8AWeGU_fwD7Q==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_unsubscribe","category":"cat facts","sg_event_id":"jIDVkh2-1yXXIJBOK-lVmg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10},{"email":"[email protected]","timestamp":1460352083,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_resubscribe","category":"cat facts","sg_event_id":"BtasI8e0rTH1GyCQHYX-Ag==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10}]

This is the code I used for decoding

function b() {
    $string = file_get_contents("/home/linux/Public/test/a.json");
    $json_a = json_decode($string, true);
    print_r($json_a);
        foreach ($json_a as $person_name) {
        print_r($person_name);
    }
}

It prints Invalid argument supplied for foreach() as error. I removed all response used only one response, then it works fine.

2
  • @Qix. I have updated my question. Commented Apr 11, 2016 at 6:11
  • So you should add multiple responses in JSON format as well; share code you are using for adding response to json file; Commented Apr 11, 2016 at 6:20

2 Answers 2

2

If you use newlines to separate each json, you could do

foreach (file($file_name) as $json_plain) {
    print_r(json_decode($json_plain, true));
}

file() could split file into an array of lines, so you can decode each line.

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

Comments

0
 <?php

$file_name = 'a.json';
foreach (file($file_name) as $json_plain) {
    print_r(json_decode($json_plain, true));
 }
?>

You can use this.

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.