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'];
Related
Ive got the following JSON:
{
"servers": [
{
"id": "f34c0185-4c9e-40fd-82f6-1d6e9a5d499e",
"name": "vm01"
},
{
"id": "d671ac7d-3b5a-4777-8510-6e8e58295061",
"name": "vm02"
},
{
"id": "h59j23cc-9ve2-4508-1277-85y1lo27562m",
"name": "vm03"
}
]
}
I also have another JSON that gives me the ID I want to search for.
For example: "d671ac7d-3b5a-4777-8510-6e8e58295061".
I want to search for the JSON Object, that contains that ID and get the value of the name key. I tried with loops and if, else's but I didn't manage to get it working.
Thanks for your help!
decode the json as array object then loop through with the ID that u want to search
<?php
$json = '{
"servers": [
{
"id": "f34c0185-4c9e-40fd-82f6-1d6e9a5d499e",
"name": "vm01"
},
{
"id": "d671ac7d-3b5a-4777-8510-6e8e58295061",
"name": "vm02"
},
{
"id": "h59j23cc-9ve2-4508-1277-85y1lo27562m",
"name": "vm03"
}
]
}';
$j = json_decode($json, true);
foreach($j['servers'] as $arr)
{
if( $arr['id'] == 'd671ac7d-3b5a-4777-8510-6e8e58295061' ) echo $arr['name'];
}
demo: https://3v4l.org/0DboX
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);
I have the following deocoded JSON array.
I need to access the "type" inside the context, as well as I need to loop through each of the values in the payload. How do I do that?
{
"RequestHeader":
{
"mess": "am putting it on my wall....",
"created_time": "2010-08-24T09:01:25+0000"
},
"context" :
{
"type": "friends,circles"
}
"payload" [ {12345},{12345} ,{2345} ]
}
I tried the following, but it doesn't work
$decoded = json_decode($json_string);
for ($i=0;$i<payload.length;++$i)
{
$id=$decoded->payload[$i];
//do some operation with the id
}
First of all the JSON you provided is invalid. Supposedly the valid one should look like this
{
"RequestHeader": {
"mess": "am putting it on my wall....",
"created_time": "2010-08-24T09:01:25+0000"
},
"context": {
"type": "friends,circles"
},
"payload": [
12345,
12345,
2345
]
}
After you've fixed the problem with JSON provider it will be quite easy to access data
<?php
$json = <<<'JSON'
{
"RequestHeader": {
"mess": "am putting it on my wall....",
"created_time": "2010-08-24T09:01:25+0000"
},
"context": {
"type": "friends,circles"
},
"payload": [
12345,
12345,
2345
]
}
JSON;
$data = json_decode($json, true);
$type = $data['context']['type'];
var_dump($type);
foreach($data['payload'] as $id) {
var_dump($id);
}
Remember to make sure you check that the data actually exists before accessing it, e.g. isset($data['context']['type']) unless you are absolutely sure in it's integrity.
When you use json_decode method, the output will be nested arrays
So for example to access context type you need to do the following
echo $decoded["context"]["type"];
And to loop on payload you need to do the following
for ($i=0;$i<$decoded["payload"].length;++$i)
{
$id=$decoded["payload"][$i];
//do some operation with the id
}
I was wondering what would be the best way to grab values from a JSON response like this?
{
"customer": {
"link": {
"url": "https://api.neteller.com/v1/customers/CUS_0d676b4b-0eb8-4d78-af25-e41ab431e325",
"rel": "customer",
"method": "GET"
}
},
"transaction": {
"merchantRefId": "20140203113703",
"amount": 2500,
"currency": "EUR",
"id": "176391453448397",
"createDate": "2014-02-03T18:50:48Z",
"updateDate": "2014-02-03T18:50:51Z",
"status": "Accepted",
"fees": [
{
"feeType": "Service_fee",
"feeAmount": 71,
"feeCurrency": "EUR"
}
]
},
"links": [
{
"url": "https://api.neteller.com/v1/payments/176391453448397",
"rel": "self",
"method": "GET"
}
]
}
I already have the response stored in a variable:
$content = json_decode($data['content']);
How do I echo these values in PHP? Let's say I want merchantRefId. Thanks!
$content = json_decode($data['content'], true);
echo $content['transaction']['merchantRefId'];
I tested this here: PHP Sandbox
echo $content->transaction->merchantRefId;
json_decode() generates an object of the standard class. You then use object notation to access the properties of that object.
Alternatively, you can use
$content = json_decode($data['content'],true);
And you'll get an associative array instead of an object. Then you'll be able to access it by element name like
echo $content['transaction']['merchantRefId'];
json_decode() will build an associative array mapping your json, so if you do
echo "<PRE>"; print_r($content);
you will see how it was mapped to the array. Accessing the data would be something like
$merchantRefId = $content['transaction']['merchantRefId'];
Check how it was mapped using the print_r() function so you know how exactly it was mapped. At first, the way I exemplified seems the right one.
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);