0

I have been reading up on posts regarding this topic, but can't for the life of me read this JSON string in PHP.

I want to fetch "summary, descripton, start, end" for each events.

 {
  "pages": {
    "current": 1,
    "total": 1
  },
  "events": [
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_1",
      "summary": "Test",
      "description": "",
      "start": "2016-07-07T04:00:00Z",
      "end": "2016-07-07T05:00:00Z",
      "deleted": false,
      "created": "2016-07-07T04:48:30Z",
      "updated": "2016-07-07T04:51:26Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "opaque",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    },
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_2",
      "summary": "hyttetur langøyene",
      "description": "",
      "start": "2016-07-08",
      "end": "2016-07-09",
      "deleted": false,
      "created": "2016-07-07T07:11:21Z",
      "updated": "2016-07-07T07:11:21Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "transparent",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    }
  ]
}

Does the JSON syntax look OK? Any help creating a foreach-loop would be very much appreciated!

var_dump for json_decode:

string(2819) "{"pages":{"current":1,"total":1},"events":[{"calendar_id":"cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw","event_uid":"evt_external_577d536cd42960ce5b87c9bf","summary":"Ullevål","description":"","start":"2016-07-04T08:30:00Z","end":"2016-07-04T09:30:00Z","deleted":false,"created":"2016-07-04T11:21:41Z","updated":"2016-07-04T11:21:41Z","actions":{"delete":"event_uid"},"location":{"description":"Bygg6 2 etasje"},"participation_status":"accepted","attendees":[],"transparency":"opaque","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw","event_uid":"evt_external_577ddfced42960ce5b88ad2d","summary":"Test","description":"","start":"2016-07-07T04:00:00Z","end":"2016-07-07T05:00:00Z","deleted":false,"created":"2016-07-07T04:48:30Z","updated":"2016-07-07T04:51:26Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"opaque","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw","event_uid":"evt_external_577de0edd42960ce5b88ad9f","summary":"Ny","description":"","start":"2016-07-07T06:00:00Z","end":"2016-07-07T07:00:00Z","deleted":false,"created":"2016-07-07T04:53:55Z","updated":"2016-07-07T04:56:13Z","actions":{"delete":"event_uid"},"location":{"description":"Sted"},"participation_status":"accepted","attendees":[],"transparency":"opaque","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ","event_uid":"evt_external_577e0098d42960ce5b88c9fe","summary":"siste arbeidsdag!","description":"","start":"2016-07-08","end":"2016-07-09","deleted":false,"created":"2016-07-07T07:11:20Z","updated":"2016-07-07T07:11:20Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"transparent","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ","event_uid":"evt_external_577e0099d42960ce5b88c9ff","summary":"hyttetur langøyene","description":"","start":"2016-07-08","end":"2016-07-09","deleted":false,"created":"2016-07-07T07:11:21Z","updated":"2016-07-07T07:11:21Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"transparent","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ","event_uid":"evt_external_577e01c1d42960ce5b88ca92","summary":"sykkeltur?","description":"","start":"2016-07-09","end":"2016-07-10","deleted":false,"created":"2016-07-07T07:16:17Z","updated":"2016-07-07T07:16:17Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"transparent","status":"confirmed","categories":[],"recurring":false}]}"

print_r for json_decode:

{
pages: {
current: 1,
total: 1
},
events: [
{
calendar_id: "cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw",
event_uid: "evt_external_577d536cd42960ce5b87c9bf",
summary: "Ullevål",
description: "",
start: "2016-07-04T08:30:00Z",
end: "2016-07-04T09:30:00Z",
deleted: false,
created: "2016-07-04T11:21:41Z",
updated: "2016-07-04T11:21:41Z",
actions: {
delete: "event_uid"
},
location: {
description: "Bygg6 2 etasje"
},
participation_status: "accepted",
attendees: [ ],
transparency: "opaque",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw",
event_uid: "evt_external_577ddfced42960ce5b88ad2d",
summary: "Test",
description: "",
start: "2016-07-07T04:00:00Z",
end: "2016-07-07T05:00:00Z",
deleted: false,
created: "2016-07-07T04:48:30Z",
updated: "2016-07-07T04:51:26Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "opaque",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw",
event_uid: "evt_external_577de0edd42960ce5b88ad9f",
summary: "Ny",
description: "",
start: "2016-07-07T06:00:00Z",
end: "2016-07-07T07:00:00Z",
deleted: false,
created: "2016-07-07T04:53:55Z",
updated: "2016-07-07T04:56:13Z",
actions: {
delete: "event_uid"
},
location: {
description: "Sted"
},
participation_status: "accepted",
attendees: [ ],
transparency: "opaque",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ",
event_uid: "evt_external_577e0098d42960ce5b88c9fe",
summary: "siste arbeidsdag!",
description: "",
start: "2016-07-08",
end: "2016-07-09",
deleted: false,
created: "2016-07-07T07:11:20Z",
updated: "2016-07-07T07:11:20Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "transparent",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ",
event_uid: "evt_external_577e0099d42960ce5b88c9ff",
summary: "hyttetur langøyene",
description: "",
start: "2016-07-08",
end: "2016-07-09",
deleted: false,
created: "2016-07-07T07:11:21Z",
updated: "2016-07-07T07:11:21Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "transparent",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ",
event_uid: "evt_external_577e01c1d42960ce5b88ca92",
summary: "sykkeltur?",
description: "",
start: "2016-07-09",
end: "2016-07-10",
deleted: false,
created: "2016-07-07T07:16:17Z",
updated: "2016-07-07T07:16:17Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "transparent",
status: "confirmed",
categories: [ ],
recurring: false
}
]
}
4
  • you can use json_decode() Commented Jul 7, 2016 at 8:05
  • Show us the code sample that supposedly decodes that JSON string. It doesn't make sense for "print_r for json_decode" to give this result. Commented Jul 7, 2016 at 9:17
  • FWIW, if you json_decode a string and var_dump the result and it says string(2819) "{"..., that means the original JSON must have been double encoded (a JSON encoded JSON string), and you need to JSON-decode it twice. Which is obviously madness and needs to be fixed at the source. Commented Jul 7, 2016 at 9:20
  • Thank you so much @deceze Double madness decoding fixed my issue! Commented Jul 7, 2016 at 9:38

2 Answers 2

3

First decode the json:

$data = json_decode($yourJsonString);

Then iterate the events and access properties if your json could be decoded correctly:

if ($data !== null) {
    foreach ($data->events as $event) {
        $summary = $event->summary;
        $description = $event->description;
        $start = $event->start;
        $end = $event->end;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

you missed a closing parenthesis for your foreach.
I would also check if json_decode($string) did not returned null before looping through the array. In fact, json_decode returns null if the json string is not correct (forbidden characters, wrong format, ...).
I tried Andreas excample, with closing parenthesis, but it did not work. json_decode is returning the json string I posted in my first post. I also added a "echo "test";" inside the foreach, but this did not get printed. It seems like the foreach loop isn't traversed.
I added the null check and missing paranthesis, thanks. @Jonny: json_decode should return an object in your case. $yourJsonString should contain the json string you posted in your question.
json_decode does return the object OK. However the foreach loop does not work.
|
1

You need to json_decode it. Then you can use something like array_reduce to extract the data you want

Here is your data

$json = <<<'EOD'
{
  "pages": {
    "current": 1,
    "total": 1
  },
  "events": [
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_1",
      "summary": "Test",
      "description": "",
      "start": "2016-07-07T04:00:00Z",
      "end": "2016-07-07T05:00:00Z",
      "deleted": false,
      "created": "2016-07-07T04:48:30Z",
      "updated": "2016-07-07T04:51:26Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "opaque",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    },
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_2",
      "summary": "hyttetur langøyene",
      "description": "",
      "start": "2016-07-08",
      "end": "2016-07-09",
      "deleted": false,
      "created": "2016-07-07T07:11:21Z",
      "updated": "2016-07-07T07:11:21Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "transparent",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    }
  ]
}
EOD;

Now run this to decode an manipulate the data

$data = json_decode($json);

$events = array_reduce($data->events, function($out,$e) {
  return array_merge($out, [[
    'summary' => $e->summary,
    'description' => $e->description,
    'start' => $e->start,
    'end' => $e->end
  ]]);
}, []);

print_r($events);

Output

// Array
// (
//     [0] => Array
//         (
//             [summary] => Test
//             [description] => 
//             [start] => 2016-07-07T04:00:00Z
//             [end] => 2016-07-07T05:00:00Z
//         )
//     [1] => Array
//         (
//             [summary] => hyttetur langøyene
//             [description] => 
//             [start] => 2016-07-08
//             [end] => 2016-07-09
//         )
// )

1 Comment

I'm afraid this didn't work eighter. My code: <?php $output = exec ( 'python cgi-bin/cal.py' ); $data = json_decode($output); $events = array_reduce($data->events, function($out,$e) { return array_merge($out, [[ 'summary' => $e->summary, 'description' => $e->description, 'start' => $e->start, 'end' => $e->end ]]); }, []); print_r($events); ?>

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.