Decode php://input json - php

I have this text from file_get_contents('php://input'):
[{"email":"text#examlple.com","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<AfxmJV-OCnw#ismon1.sendgrid.net>","timestamp":16813363}]
I need to get single element like email or event, but haven't been able to get json_decode and others to work.
$obj = json_decode(file_get_contents('php://input'));
How can I reference a single element in the json data?

You have an array, so, you need to get the first element:
$json = '[{"email":"text#examlple.com","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<AfxmJV-OCnw#ismon1.sendgrid.net>","timestamp":16813363}]';
$obj = json_decode($json);
echo $obj[0]->email; //output text#examlple.com

Here you go, here's a fully working answer:
<?php
$str = '[{"email":"text#examlple.com","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<AfxmJV-OCnw#ismon1.sendgrid.net>","timestamp":16813363}]';
$obj = json_decode($str);
echo $obj[0]->email;

Related

How to take out int value from JSON object?

I'm working with Laravel 5 right now and I have the following problem. I've got response from DB query:
[{"id":1}]
and I want to take out 1 as int or string. Any ideas?
I've tried to solve this like follows:
$json = (DB query);
$data = json_decode($json);
$final = $data[0]->id;
and response is :
json_decode() expects parameter 1 to be string, array given
This is all you need.
$response = json_decode($response); // Decode the JSON
$string = $response[0]->id; // Save the value of id var
As you say, the string you have is in JSON format, so you need to use json_decode to access it in PHP.
The square brackets refer to an array, and the braces refer to an object within that array, so what you're looking for is the id value of the first element (i.e. element 0) in the array.
<?php
$json = '[{"id":1}]';
$data = json_decode($json);
echo $data[0]->id;
// 1
Try this
$json_array = '[{"id":1}]';
$data = json_decode($json_array);
print_r($data); // To display the result array
You just need to decode it, if I'm not misunderstanding your questions.
$json = '[{"id":1}]';
$decodedObject = json_decode($json);
Then you can loop through the aray and do something with your data:
foreach($decodedObject as $key => $value) {
$id = $value->id;
}
If you're using Laravel, though, why not use Eloquent models?

How to get a Particular field from an object array in a text file

I am trying to get only the refresh_token field from the text file using file_get _contents. please anyone solve this.
{"access_token":"XXXX","token_type":"bearer","expires_in":3600,"refresh_token":"XXXX"}
For json manipulation you should use json_decode
$str= file_get_contents(some.txt);
$array = json_decode($content);
echo $array->access_token;
Thats a json string
$content = file_get_contents([...]);
$arr = json_decode($content);
echo $arr->access_token;

How to get a thing from a Json text?

i want to know how can i extract a word from a json encoded or decoded.
Example:
From:
{"51973658":{"id":51973658,"name":"Covrigel","profileIconId":748,"summonerLevel":30,"revisionDate":1419865098000}}
I want to see just "Covrigel".
Is that possible?
This is a simple array. You can access that by something like this,
echo $array_name['51973658']['name'];
EDIT after question change:
$json = json_decode($json_array, true);
echo $json['51973658']['name'];
All you have to do is convert it to a PHP array.
$json = '{"51973658":{"id":51973658,"name":"Covrigel","profileIconId":748,"summonerLevel":30,"revisionDate":1419865098000}}';
$json = json_decode($json, $array = true);
echo $json['51973658']['name'];
Decode the JSON as an associative array then echo the value you need.
<?php
$json = '{"51973658":{"id":51973658,"name":"Covrigel","profileIconId":748,"summonerLevel":30,"revisionDate":1419865098000}}';
$data = json_decode($json, true);
echo $data['51973658']['name'];
?>

How do I parse a JSON object in PHP?

I've got a JSON object that I'm sending to a PHP script and I'm having trouble parsing the JSON. Here's the POST request:
http://mywebsite.com?action=somefunction&{%22id%22:1,%22Name%22:%22Mike%22}
And here's my PHP function, which obviously doesn't work:
$data = $_GET['data'];
$obj = json_decode($data);
echo $obj->Name;
die();
The end goal is to extract the name "Mike" from the URL string. Any suggestions?
Try taking a look at what PHP is outputting from json_decode():
$data = $_GET['data'];
$obj = json_decode($data);
var_dump($obj);
Your code itself works fine: http://ideone.com/0jsjgT
But your query string is missing the data= before the actual JSON. This:
http://mywebsite.com?action=somefunction&{%22id%22:1,%22Name%22:%22Mike%22}
should be this:
http://mywebsite.com?action=somefunction&data={%22id%22:1,%22Name%22:%22Mike%22}
you should do
echo $obj->{'Name'};
This also is a duplicate question of Echo data json by json_decode

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