0

this is the JSON below I want to parse

{"events": [{"eventId": 2, "payload": {"chat": {"chatId": "683473108@chat.agent", "title": "TrojanTest", "type": "group"}, "from": {"firstName": "Khan", "nick": "Mr.Tr0J4n", "userId": "751401693"}, "msgId": "6895591502924218369", "text": "524545", "timestamp": 1605505008}, "type": "newMessage"}], "ok": true} I want to parse msgId and chatId from this JSON.

How can I do that in PHP?

Akib Khan
  • 33
  • 6

1 Answers1

0

decode json string to array:

$jsonString = '{"events": [{"eventId": 2, "payload": {"chat": {"chatId": "683473108@chat.agent", "title": "TrojanTest", "type": "group"}, "from": {"firstName": "Khan", "nick": "Mr.Tr0J4n", "userId": "751401693"}, "msgId": "6895591502924218369", "text": "524545", "timestamp": 1605505008}, "type": "newMessage"}], "ok": true}';

$arr = json_decode($jsonString, true);

fetch from array:

$chatId = $arr['events'][0]['payload']['chat']['chatId'];
$msgId = $arr['events'][0]['payload']['msgId'];
var_dump($chatId);
var_dump($msgId);

the result will be:

string(20) "683473108@chat.agent"
string(19) "6895591502924218369"
igorb0214
  • 67
  • 2
  • 2
  • 9