1

im trying to access the ID and SENT from this JSON but is not working for me.

UPDATED

$json = '{
 "response": {
 "sent": true,
 "message": "Sent to 57304",
 "id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
 }
}';
json_decode($json);
$id=$json->id;
$sent=$json->sent;
echo "</br> id:".$id."</br>";
echo "</br> sent:".$sent."</br>";
3
  • You need to wrap the json string in json_decode() Commented Oct 21, 2021 at 0:21
  • @mulquin just updated and still not working with json_decode() Commented Oct 21, 2021 at 0:32
  • Next time you can var_export($json) to see what it has and discover your mistake. That's the debug way. Commented Oct 21, 2021 at 0:53

2 Answers 2

1

Use the php json_decode() function. Like this:

$json = '{
 "response": {
 "sent": true,
 "message": "Sent to 57304",
 "id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
 }
}';
$json = json_decode($json, true);
$json = (object) $json['response'];
echo $json->sent; // output: 1
echo $json->id; // output: "gBEGVzBChXFYAgmcOrfFpGem8qw"

1 is the equivalent for a boolean value of true

answered Oct 21, 2021 at 0:31
Sign up to request clarification or add additional context in comments.

2 Comments

Thank a lot this worked !
Nothing is happening, I'm glad it works for you. @Andres Carvajal
0

It Works,There are two label of JSON object,so you cant access inner object directly. you should assign json_decode($json) into a php object like $obj=json_decode($json); here is the working file

<?php
$json = '{
 "response": {
 "sent": true,
 "message": "Sent to 57304",
 "id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
 }
 }';
 // convert assign json data into php object
 $obj=json_decode($json);
 //All the inner key can be access throug 'response' object
 $id=$obj->response->id;
 $sent=$obj->response->sent;
 
 
 echo "</br> id:".$id."</br>";
 echo "</br> sent:".$sent."</br>";

answered Oct 21, 2021 at 1:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.