I am trying to the balance in a PHP object, I tried everything but am getting array to string conversion error, here is the string
{
"status": true,
"message": "Balances retrieved",
"data": [
{
"currency": "NGN",
"balance": 0
}
]
}
its a response from a remote server, i am trying to get the balance, i tried
$response->data->balance
json_decode($response)->data->balance
json_decode($response[0])->data->balance
but none of them worked, the response from the remote server is stored in a variable called $response
How can i get the balance from the response
Data is an array
json_decode($response)->data[0]->balance
Take a look at this example, that I created: http://sandbox.onlinephpfunctions.com/code/414f23b1a09221ed5bd7f299f5eb73a2f9f932b1
First you decode the JSON string
Then access data array at the index 0, so data[0]
And then data[0]->balance, access object property 'balance'
Gerard's answer is right.
Also you can naviguate the result of a json_decode function in array mode.
<?php
$json = '{
"status": true,
"message": "Balances retrieved",
"data": [
{
"currency": "NGN",
"balance": 0
}
]
}';
$response = json_decode($json);
echo $response["data"][0]["balance"];
echo $response->data[0]->balance;
Using a variable to register the decode answer will save you time.
Related
I am trying to return JSON in a controller using this line:
return response()->json( $return );
$return contains two key/value pairs.
When I try to validate the response in a test, it fails because the response does contain additional fields:
Unable to find JSON:
[{
"statusCode": 665,
"msg": "auth missing"
}]
within response JSON:
[{
"headers": [],
"original": {
"statusCode": "665",
"msg": "auth missing"
},
"exception": null
}].
I have no idea how the additional keys get there and why the source array is nested in the source key.
How can I make sure that only the array that I pass to response()->json is returned?
Answering my own question here:
The problem was that $return was already a json encoded object. So this weird formatting is the result of converting json into json.
I have a request to the Facebook Graph API that returns a post from a facebook account.
I am simply trying to access the data instance of the object returned in PHP but everything I have tried returns NULL.
Sample response
{
"data": [
{
"id": " 111111111111111111100000",
"message": "Coming soon #PERFECTFIT 05.07.17 👀\nRegister to be one of the first to find out what it is here ⬇️\nhttp://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"created_time": "2017-06-26T17:39:20+0000",
"link": "http://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"full_picture": "https://scontent.xx.fbcdn.net/v/t39.2147-6/19284954_1592534984092755_4946207882807869440_n.jpg?oh=56cc96435f423cec31962966b6f689c2&oe=59DB08B6"
}
]
}
I want to get at the array of objects data provides so I can MVC the data returned in a larger response.
This doesn't currently work:
$response->data; // returns null
$response[0]->data; // returns null
$response->data[0]; // returns null
Feel I'm missing something obvious.
first decode it with json_decode then trying to access the data object
$res = json_decode($response);
print_r($res->data);
print_r($res->data[0]->id);
print_r($res->data[0]->message);
code
you have to use first json decode function so you can get the data from the json response.
$response = '{
"data": [
{
"id": "143384725674462_1592535354092718",
"message": "Coming soon #PERFECTFIT 05.07.17 👀\nRegister to be one of the first to find out what it is here ⬇️\nhttp://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"created_time": "2017-06-26T17:39:20+0000",
"link": "http://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"full_picture": "https://scontent.xx.fbcdn.net/v/t39.2147-6/19284954_1592534984092755_4946207882807869440_n.jpg?oh=56cc96435f423cec31962966b6f689c2&oe=59DB08B6"
}
]
}';
$response = json_decode($response);
print_r($response->data[0]->id);
print_r($response->data[0]->message);
So what I have here is a JSON response from my PHP cURL, so I'm trying to get the specific group of array under location depending where is the identifier value I'm checking.
{
"status": "SUCCESS",
"response": {
"offset": 0,
"max": 50,
"count": 2,
"locations":[
{
"identifier":"54320",
"id": 503892,
"name":"The Foo"
},
{
"identifier":"54321",
"id": 503893,
"name":"The Bar"
}
]
}
This is what I've done so far and what I'm trying to do. So I already parse the json above and put it on foreach. Note the identifier is not always an int it can also be string.
$parsed_json = json_decode($phpCurlResponse, true);
$parsed_json = $parsed_json["response"]["locations"];
foreach($parsed_json as $key => $pj){
if($pj['identifier'] == "54320"){
echo $pj['name'].' & '.$pj['id'];//this should display The Foo & 503892
}
}
I have tried this one and it can only see the first group of array under locations but when I change the identifier value from 54321 to 112233 the response will be Identifier do not exist. can you help me how we achieve this.
Your JSON is invalid.
There shouldn't be a comma after the last object in the locations array.
There should be a } after the locations closing array bracket (]).
So is the valid JSON.
{
"status": "SUCCESS",
"response": {
"offset": 0,
"max": 50,
"count": 2,
"locations": [
{
"identifier":"54320",
"id": 503892,
"name":"The Foo"
},
{
"identifier":"54321",
"id": 503893,
"name":"The Bar"
}
]
}
}
Sorry for the premature posting but I want you to know that there's nothing wrong with the code above its just had some typo error on the json response I typed, anyways thank you for the help guys. :) So that's one possible way to get an specific set of array inside an object depending where's the key value you looking for.
I'm trying to retrieve team1 score however i cant seem to figure out how to output this. So far i've got this to work where $obj is the json output.
$obj->recent
JSON
"recent": [
[
{
"match_id": "64886",
"has_vods": false,
"game": "dota2",
"team 1": {
"score": "",
"name": "Wheel Whreck While Whistling",
"bet": "7%"
},
"team 2": {
"score": "",
"name": "Evil Geniuses DotA2",
"bet": "68%"
},
"live in": "1m 42s",
"title": "Wheel Whreck... 7% vs 68% Evil...",
"url": "",
"tounament": "",
"simple_title": "Wheel Whreck... vs Evil...",
"streams": []
}
]
You need to use json_decode(); This function returns proper object with arrays and objects inside. Now you need check what is an object and what is an array.
$obj = json_decode($obj, true);
$obj->recent; //array
$obj->recent[0]; //first element of array
$obj->recent[0][0]; //first element of second array
$obj->recent[0][0]->{'team 1'}; //access to object team 1
$obj->recent[0][0]->{'team 1'}->score; //access to property of object team 1
You can find this helpful to understand what happens;
You can also check example on json_decode documentation
If you use var_dump function on $obj it will show you what is an array and what is an object.
You'll want to use json_decode to get that into an array. It looks like recent is an array of arrays of objects. So, you'll do something like
$json = json_decode($obj->recent, true);
$team1 = $json[0][0]['team 1']; //should return array
$score = $team1['score']
edit: Thanks for the comment, was missing a true as the second param in json_decode
I am using "slim-json-api" in a project to access some data in a MySQL database. I am trying to pull back a single row and return this to the response, but I am getting no data back.
$app->get('/topics/:id', function($id) use ($app) {
$topic = ORM::for_table('topics')->find_one($id);
$posts = ORM::for_table('posts')->where('topics_id', $id)->find_array();
$app->render(200, array(
"topic" => $topic,
"posts" => $posts
));
});
The result of this is as follows:
{
"topic": {},
"posts": [
{
"id": "1",
"user_id": "1",
"topics_id": "1",
"post_time": "2014-03-17 12:44:24",
"post_subject": "I like this!",
"post_text": "This is the body of an amazing post!"
}
],
"error": false,
"status": 200
}
The annoying this about this is that I get a fully populated "posts" object and I know the data is present and correct in the DB. It is as if the array of results for "posts" is not an array of single returned value types.
Any help would be much appreciated.