parse json in php, json objects and json arrays [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parse a JSON string using PHP
this is my data object
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt"
},
{
"translatedText": "Hallo Berlin"
}
]
}
}
how do I parse this using PHP?
this is a jsonObject that contains jsonObject("data") that contains jsonArray that contains jsonObjects at each index that contains key/value "translatedText"
this is what I have and my assumption
$jsonResult = json_decode($data);
$translated_text = $jsonResult->data->translations[0]->translatedText;`

$array = json_decode($json_element, true);
to make associative array.

I'm think this is what it would be. json_decode does not parse to a PHP object, but to just an array.
$jsonResult = json_decode($data);
$translated_text = $jsonResult['data']['translations'][0]['translatedText'];

$array = json_decode($json_element);

Related

How can I read this nested json in php? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I have this JSON
{
"AED_USD": {
"2019-08-01": 0.272242
}
}
When dumping this as an array I have:
array(1) { ["AED_USD"]=> array(1) { ["2019-08-01"]=> float(0.272242) } }
I am trying to reach get the float value of 0.272242.
How do I get this value in PHP?
You have to json_decode the string with true as second parameter to make it an array, and then just echo what you want using the name of the keys you have. Like so:
$string = '{
"AED_USD": {
"2019-08-01": 0.272242
}
}';
$decode = json_decode($string, true);
echo $decode['AED_USD']['2019-08-01'];
To get the value in PHP, you can do
$value = $yourArray['AED_USD']['2019-08-01'];
If you want more information: https://www.php.net/manual/pt_BR/language.types.array.php

How to extract a series of variables from string of data in php? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
The following chunk of data is produced by a webpage along with other html data;
"search":{"searchResults":{"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
{"id": //and so on//
"}
]}}
How to extract multiple variables from this string like data like "id", "rating" etc., to be able to print it on another php page?
You can use json_decode to convert JSON to the array and then iterate through the array
$json = '{
"search":
{
"searchResults":
{
"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
]
}
}
}';
$jsonToArray = json_decode($json,true);
foreach($jsonToArray['search']['searchResults']['results'] as $v){
echo $v['id'];
print_r($v['rating']);echo '<br/>';
}

Create array with only tag - Json decode [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);

php json decode object with array of multiple objects [duplicate]

This question already has answers here:
Read JSON Data Using PHP [duplicate]
(5 answers)
Closed 7 years ago.
I am unable to get values of array of this json... how can i have all these values seperately!!
{
"id": "jai",
"pwd": "123",
"user": [
{
"fname": "jai",
"lname": "gupta"
},
{
"fname": "sameer",
"lname": "seth"
}
],
"college": "vit"
}
$myArray = json_decode($json, true);
var_dump($myArray['id']);
var_dump($myArray['user'][0]['fname']);
You can decode these values from json to object.
$result = json_decode('{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}');
and acccess like below:
$result->id;
$result->pwd;
You can access each "value" by accessing regular php array key value pairs.
$jsonn = '{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}';
$new = json_decode($jsonn, true);
$id = $new['id'];
$user = $new['user'];
..... and so on.
Hope this helps.
Cheers!

Read JSON Data Using PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 months ago.
Solr returns response in following JSON format.
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"indent":"on",
"start":"0",
"q":"*:*",
"wt":"json",
"version":"2.2",
"rows":"10"}},
"response":{"numFound":3,"start":0,"docs":[
{
"student_id":"AB1001",
"student_name":[
"John"]
},
{
"student_id":"AB1002",
"student_name":[
"Joe"]
},
{
"student_id":"AB1003",
"student_name":[
"Lorem"]
}]
}}
What will be the simple way to read student_id, student_name using PHP?
Use $obj = json_decode($yourJSONString); to convert it to an object.
Then use foreach($obj->response->docs as $doc) to iterate over the "docs".
You can then access the fields using $doc->student_id and $doc->student_name[0].
PHP has a json_decode function that will allow you to turn a JSON string into an array:
$array = json_decode($json_string, true);
$student_id = $array['response']['docs'][0]['student_id'];
...
Of course, you might want to iterate through the list of students instead of accessing index 0.
$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...
Why not just use one of the PHP clients for Solr, or the PHP response writer? See http://wiki.apache.org/solr/SolPHP
$json_a = json_decode($string, TRUE);
$json_o = json_decode($string);
#array method
foreach($json_a['response']['docs'] as $students)
{
echo $students['student_id']." name is ".$students['student_name'][0];
echo "<br>";
}
#Object Method`enter code here`
foreach($json_o->response->docs as $sthudent_o)
{
echo $sthudent_o->student_id. " name is ".$sthudent_o->student_name[0];
echo "<br>";
}

Categories