In one of my applications I get a JSON format response data from a REST API. Which is as similar as the following:
{
"Response": {
"user_id": "12003",
"username": "Shimul",
"status": "active"
},
"ErrorMessage": "",
"Status": 0
}
From this response, I just need to fetch user_id to process further operations in my application. But I can't identify how to decode this using PHP json_decode()
Can anyone tell me how can I decode this using PHP and fetch only user_id from this response?
Thanks
json_decode take 2 parameters.
(string) json
(bool) assoc. If TRUE, the function will return array else object.
so you just need to do is:
<?php
$json = '{
"Response": {
"user_id": "12003",
"username": "Shimul",
"status": "active"
},
"ErrorMessage": "",
"Status": 0
}';
// By converting into array..
$array = json_decode($json,TRUE);
$user_id = $array['Response']['user_id'];
//---------OR---------
// Using Object
$obj = json_decode($json);
$user_id = $obj->Response->user_id;
?>
I hope this will help.
You don't show us what you tried so far and why it failed, but it's as simple as that:
<?php
$json = '{
"Response": {
"user_id": "12003",
"username": "Shimul",
"status": "active"
},
"ErrorMessage": "",
"Status": 0
}';
var_dump(json_decode($json)->Response->user_id);
Related
I'm using zoho crm..
I get the json data below returned from a query
however I'm struggling to get the value of a string inside the returned data
Here is a sample of the data returned
"response": {
"result": {
"Deals": {
"row": {
"no": "1",
"FL": [
{
"val": "DEALID",
"content": "3508588000000206039"
},
{
"val": "SMOWNERID",
"content": "3508588000000176021"
},
{
"val": "Amount",
"content": "5000"
}
I'm trying to get the Amount value
Here is the PHP code
$json = file_get_contents($url);
$obj = json_decode($json);
$amount = $obj->result->Deals->row->FL['Amount'];
echo 'Deal Amount : £'.$amount;
Thanks In Advance
You need to change a bit
$amount = $obj->response->result->Deals->row->FL[2]->content;
//--------------^index------------------------^index---^column name need to be correct---
Dealing with Zoho response could get messy. You could use this library to help smoothen things for you.
In the meantime, $obj->response->result->Deals->row->FL[2]->content; should do the trick for you.
I'm trying to get data from the following JSON file using PHP
I Want To Get All URls (link) ,
http://www.google.com
http://www.bing.com
Json
Here is what I tried
PHP
Thanks
You cannot access an object property using a number, it must be a string.
It is much easier to output json_decode as an array, and access properties that way. To do this, put true as the second parameter.
<?php
$json = '
{
"news": {
"name": "yahoo",
"url": "https://yahoo.com"
},
"links": [
{
"id": "1",
"url": "https://google.com"
},
{
"id": "2",
"url": "https://bing.com"
}
]
}';
$decode = json_decode($json, true);
echo $decode['links'][0]['url'];
can someone please help me to add data to following json.
$temp='{
"custId": 123,
"data": [
{
"code": "abc1"
},
{
"code": "abc2"
}
],
"id": 102
}';
data to be added
$data1='{
"code": "abc3"
}';
Desired result
$temp='{
"custId": 123,
"data": [
{
"code": "abc1"
},
{
"code": "abc2"
},
{
"code": "abc3"
}
],
"id": 10
}';
I can append data to $temp, but can someone please help to add data to specific position with php. Thank you in advance. (I've tried json_decode($temp, true) and then finding the location where data1 is to be added but failed).
You can convert your data to a normal PHP array:
$array = json_decode($temp, true);
then just add or change anything you want:
$array["data"][] = array(["code"] => "abc3");
and re-encode to json:
$temp = json_encode($array);// encode to json
http://php.net/manual/en/function.json-decode.php
first, you need to convert the JSON into PHP array like this:
$array = json_decode($temp, true);
Then, it's as simple as:
$array['data']['code'] = 'abc3';
If the string that you want to add is still a JSON, you have to convert that one in an array as well so:
$arr_to_add = json_decode($data1, true);
$array['data'][] = $arr_to_add;
In the end, of course, encode again like this:
$final_json = json_encode($array);
Hope this helps! :D
First you need to decode the json with json_decode. This returns a PHP variable.
Add desired values.
Use json_encode to get the json with new values. Returns a string containing the JSON.
you can try this:
<?php
$temp='{
"custId": 123,
"data": [
{
"code": "abc1"
},
{
"code": "abc2"
}
],
"id": 102
}';
$data1='{
"code": "abc3"
}';
$jarr=json_decode($temp,true);
$appArr=json_decode($data1,true);
$desire=array_merge($jarr,$appArr);
//print_r($desire);
echo json_encode($desire);
As a test, this JSON data is being POSTed to my website:
{
"order": {
"id": null,
"created_at": null,
"status": "new",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 2263,
"currency_iso": "USD"
},
"custom": "123456789",
"button": {
"type": "buy_now",
"name": "Test Item",
"description": null,
"id": null
},
"transaction": {
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
}
}
}
Since it is being sent server-to-server I cannot visibly see the data. I have tried sending the $_POST array to a text file, but it comes up blank. What I think I need to do is:
$data = json_decode($jsonData);
But how do I set the variable $jsonData?
You can try to use wrappers for reading raw POST query.
$data = file_get_contents("php://input");
Have you tried this, store the sting obtained to a variable and then decoding it?
$postedJsonData= '{"order":{"id":null,"created_at":null,"status":"new","total_btc":
{"cents":100000000,"currency_iso":"BTC"},"total_native":
{"cents":2263,"currency_iso":"USD"},"custom":"123456789","button":
{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":
{"hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}';
var_dump(json_decode($postedJsonData, true));
The true parameter would return an associative array
$data = json_decode($jsonData, True);
As a test, this JSON data is being POSTed to my website:
{
"order": {
"id": null,
"created_at": null,
"status": "new",
"total_btc": {
"cents": 100000000,
"currency_iso": "BTC"
},
"total_native": {
"cents": 2263,
"currency_iso": "USD"
},
"custom": "123456789",
"button": {
"type": "buy_now",
"name": "Test Item",
"description": null,
"id": null
},
"transaction": {
"hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"confirmations": 0
}
}
}
Since it is being sent server-to-server I cannot visibly see the data. I have tried sending the $_POST array to a text file, but it comes up blank. What I think I need to do is:
$data = json_decode($jsonData);
But how do I set the variable $jsonData?
You can try to use wrappers for reading raw POST query.
$data = file_get_contents("php://input");
Have you tried this, store the sting obtained to a variable and then decoding it?
$postedJsonData= '{"order":{"id":null,"created_at":null,"status":"new","total_btc":
{"cents":100000000,"currency_iso":"BTC"},"total_native":
{"cents":2263,"currency_iso":"USD"},"custom":"123456789","button":
{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":
{"hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}';
var_dump(json_decode($postedJsonData, true));
The true parameter would return an associative array
$data = json_decode($jsonData, True);