How to get label data from JSON from file contents - php

$json = file_get_contents('module=API&method=Referrers.getReferrerType&format=json&period=day&date=yesterday&disableLink=1&idSite=3');
$data = json_decode($json,true);
json data:
[{"label":"Direct Entry","nb_uniq_visitors":526,"nb_visits":593,"nb_actions":768,"nb_users":0,"max_actions":32,"sum_visit_length":83153,"bounce_count":513,"nb_visits_converted":0,"segment":"referrerType==direct"},{"label":"Search Engines","nb_uniq_visitors":230,"nb_visits":235,"nb_actions":631,"nb_users":0,"max_actions":71,"sum_visit_length":52233,"bounce_count":150,"nb_visits_converted":0,"segment":"referrerType==search","idsubdatatable":2},{"label":"Websites","nb_uniq_visitors":7,"nb_visits":7,"nb_actions":20,"nb_users":0,"max_actions":5,"sum_visit_length":835,"bounce_count":1,"nb_visits_converted":0,"segment":"referrerType==website","idsubdatatable":3}]
I'm trying to display each label for the referrer type.
Direct Entry
Search Engines
Websites
$data[0]->label; doesn't work
$data->label; doesn't work
$data->label[0]; doesn't work

You passed true to the second parameter to json_decode(). That makes it return an associative array. Thus, you can simply use the data like this :
$data[0]["label"]

You can try this, as you're converting the json data to associative array (nested), by passing true into the 2nd argument of json_decode method:
$json = "[{\"label\":\"Direct Entry\",\"nb_uniq_visitors\":526,\"nb_visits\":593,\"nb_actions\":768,\"nb_users\":0,\"max_actions\":32,\"sum_visit_length\":83153,\"bounce_count\":513,\"nb_visits_converted\":0,\"segment\":\"referrerType==direct\"},{\"label\":\"Search Engines\",\"nb_uniq_visitors\":230,\"nb_visits\":235,\"nb_actions\":631,\"nb_users\":0,\"max_actions\":71,\"sum_visit_length\":52233,\"bounce_count\":150,\"nb_visits_converted\":0,\"segment\":\"referrerType==search\",\"idsubdatatable\":2},{\"label\":\"Websites\",\"nb_uniq_visitors\":7,\"nb_visits\":7,\"nb_actions\":20,\"nb_users\":0,\"max_actions\":5,\"sum_visit_length\":835,\"bounce_count\":1,\"nb_visits_converted\":0,\"segment\":\"referrerType==website\",\"idsubdatatable\":3}]";
$data = json_decode($json, true);
print_f($data[0]['label']);
Hope this helps!

Related

How to get value from JSON encode in PHP?

i have searched and searched but i don't know where i'm wrong getting a value from JSON encode, can you help me? Please don't kill me, i'm a newbie :)
My php:
<?php
$data = json_decode("document.json", true);
$getit = $data["likes"];
My JSON:
[{
"title" : "MYTITLE",
"image" : "MYIMAGE",
"likes" : 0
}]
EDIT
Thanks for the help this is now working!
json_decode expects an string, not an filename - so you have first get the contents of the given file. This could be easily achieved with file_get_contents.
Your current json structure contains an array(with currently only one element), which contains an object. So if you want the likes, you have to read the first element of the result array and of that(an associative array), the likes.
$data = json_decode(file_get_contents($filename), true);
$likes = $data[0]['likes'];
If you have more than one object in the given json file, you could loop over the data with foreach
foreach ($data as $element) {
echo "likes of {$element['title']}: {$element['likes']}\n";
}
For json_decode you have to pass JSON string, not a file name. Use file_get_contents to get JSON content and then decode it.
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]['likes'];
Your JSON is an array of objects, you first need to access the first element of your array :
$data[0]
Then you can access the key you want :
$getit = $data[0]['likes'];
Plus, as stated in a comment above, you need to pass a string to json encode, here is an code sample that should suit your needs :
<?php
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]["likes"];
Hope this helps.

Error Parsing JSON file with PHP

I have a string like this:
{
"update_id":659510742,
"message":{
"message_id":178,
"from":{
"id":110910409,
"first_name":"S.M_Emamian"
},
"chat":{
"id":-57184742,
"title":"Vvggg",
"type":"group"
},
"date":1446970836,
"new_chat_participant":{
"id":131677747,
"first_name":"Shadyab",
"username":"Shadyabbot"
}
}
}
now, I would like to get first_name. how can I get that ?
I tested below code, but it doesn't work :
$json_a = json_decode($content, true);
$first_name = $json_a->message->from->first_name; //it returns nothing.
$json_a = json_decode($content, true);
^^^^
That true forces the output of json_decode to be an array, but you are trying to treat it like an object. Remove that argument or use array syntax:
$first_name = $json_a['message']['from']['first_name'];
use only json_decode($content) remove true
try this code :-
$json_a = json_decode($content);
echo $json_a->message->from->first_name;
True convert the output of json_decode to be an array
$json_a = json_decode($content, true);
$first_name = $json_a->message->from->first_name; //it returns nothing.
That's because the function returns an associative array, and not an object.
By using -> you're trying to access object properties of $json_a. What you're really looking for is:
$json_a["message"]["from"]["first_name"]
This way you're accessing the associative array values by their key, and not trying to access object properties.
I hope this helped,
Sebastian
$json_a = json_decode($content);
$first_name = $json_a->message->from->first_name;
or
$json_a = json_decode($content,true);
$first_name = $json_a[message][from][first_name];
You're passing true as the second argument to json_decode which tells it to return an associative array rather than an object.
Either remove the second argument (or change it to the default, false), or access the fields using array syntax:
$first_name = $json['message']['from']['first_name'];
json_decode() is a php function used to decode json into either php standard object or an associative array dependent upon argument passed to it
When true it will return an associative otherwise by default it returns standard PHP object
According to your requirement please try executing following code snippet for retrieving first name from json document.
$json_a = json_decode($content, true);
$first_name = $json_a['message']['from']['first_name'];
For more detailed description regarding parsing json documents in php please refer the documentation in following URL
http://php.net/manual/en/function.json-decode.php

extract details from from string

I have a string in my database along with type varchar
{"available":"","bind":"0","hours":{"00:00":{"available":"","bind":"0","info":"","notes":"","price":"","promo":"","status":"none"}},"hours_definitions":[{"value":"00:00"}],"info":"","notes":"","price":"100","promo":"","status":"available"}
How do I extract the price from this?
That string is valid JSON data, so you all you need to do is decode to an array for PHP to read. Like so:
$data = json_decode($string);
$price = $data->price;
To force it to be an array instead of an object, pass true to the second argument of json_decode, like so:
$data = json_decode($string, true);
$price = $data['price'];
That looks like JSON to me, so the steps are really simple:
1. Use json_decode($response, true) to convert from JSON to an array.
2. Treat as a normal array.
As such, the following code should work fine for you
$info = json_decode($jsonCode, true);
$price = $info['price'];
And the price will be stored in $price, or just $info['price'] if you don't need another variable for it.
json_decode()'s second argument means that it will return associative arrays (like the one you have here) as an array, rather than an object. It's usually more useful that way, however you could always set it to false (or just leave it out, as false is the default).
See json_decode's PHP documentation page for more info on its arguments.

PHP: Accessing dictionaries from a JSON string

I'm writing a simple PHP script that makes an API call that returns JSON. However, I'm having trouble figuring out how to take that JSON string, convert it into a dictionary, and access nested dictionaries/data within it.
Here's what I have so far:
<?php
$id = $_REQUEST['id'];
$url = http://exampleURLThatReturnsJSONString.com
$rawData = file_get_contents($url);
I've read that you should use something like $decodedData = json_decode($rawData)
, but I'm not sure what to do next, especially if I want to access nested dictionaries with a key like Schedule.
Any help would be greatly appreciated, thanks!
json_decode($json, $assoc = false) converts the json string into an object by default, or into an array if you specify $assoc = true
If you have $assoc = false then you must access the values by $decoded_data->key. Whereas if you have $assoc = true then you can do $decoded_data['key']
You can just access the decoded data like this:
echo $decodedData['key'];
This wil echo the value of the item in the dictionary with key 'key'. Nested values can be accessed like this:
echo $decodedData['key1']['key2']['...'];
You can always use var_dump to show what's inside the result. Also, read the documentation on json_decode for more information.
json_decode return array or object to which access is like to array.
http://php.net/manual/en/function.json-decode.php

Reading multiple json values with php

I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for

Categories