I am trying to modify my JSON file with a PHP solution :
From this file :
[
{
"id":40764465700014,
"Pays":"France",
"lat":43.221401,
"long":6.1424990000000435
}
]
To this file :
[
{"id" : "40764465700014",
"Pays" : { "add" : "France" },
"lat" : { "add" : 43.221401 },
"long" : { "add" : 6.1424990000000435}
}
]
Do you have an idea ?
Thanks
Just use json_decode() and convert it to array , then just encode the array into json using json_encode(). Use the code below
<?php
$json = '[
{
"id":40764465700014,
"Pays":"France",
"lat":43.221401,
"long":6.1424990000000435
}
]';
$array= json_decode($json,true);
echo json_encode($array[0]);
Hope this helps you
Related
I have one json i wanted to print "formatted_address" element from that json
Json
{
"html_attributions" : [],
"results" : [
{
"formatted_address" : "Narayan Peth, Pune, Maharashtra 411030, India",
"geometry" : {
"location" : {
"lat" : 18.515797,
"lng" : 73.852335
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "3a25975b3806df28aa79ac4a8d954c307be4aa57",
"name" : "Aditya Medical",
"place_id" : "ChIJJxwmOHDAwjsRjRDO4LnGJ-I",
"reference" : "CmRSAAAA3E7ih55-2BZjRQcw_URQ2gwi8eWb5HU6hdfNUj_TqtDJ7TtASVMowcuWMkohNjp7F6UKuGsMuR-IlzZEt4YUJyzNxzWg-TYy6hyN8P5n2asAO6ztZeU3oHZdH7OBFFW_EhBe4cQbAU99oILcDmvv_gOhGhR7jzP0Z9-mDrncd5Gr9hOY7aOqRg",
"types" : [ "pharmacy", "health", "store", "point_of_interest", "establishment" ]
}
],
"status" : "OK"
}
I tired to print using but unable to print it.
foreach (json_decode($address[0]->Response) as $obj){
print_r($obj['results']['formatted_address']);
}
You need to set second param as true to get json as array. Also yourformatted_address is agin inarray so need to pass index in it
foreach (json_decode($address[0]->Response, true) as $obj){
print_r($obj['results'][0]['formatted_address']);
}
DEMO
That can be a solution :
$jsonAsArray = json_decode($yourJson, true);
$results = $array["results"][0];
var_dump($results['formatted_address']);
Good luck
Try
foreach (json_decode($address[0]->Response, true) as $obj){
print_r($obj['results']['formatted_address']);
}
json_decode has a second parameter to determine the returned result format - object or array.
I Try like #Plamen Nikolov, but but it doesn't work. And I Try change 'True' to 1 it's work!
foreach (json_decode($address[0]->Response, 1) as $obj){
print_r($obj['results']['formatted_address']);
}
Try
foreach (json_decode($address[0]->Response)->results as $obj){
print_r($obj->formatted_address);
}
json_decode($address[0]->Response) will give you an object. You should not use like array. so you should use "->" instead of array form. Better you can put the result on foreach and get the data of formatted_address from that $obj
--
I want to read json file from database directory, but give The Response content must be a string or object implementing __toString(), "boolean" given. error. Bellow is my code :
$json = file_get_contents('database/query_templates/course.json');
$data = json_decode($json, TRUE);
echo '<pre/>'; print_r($data); exit;
course.json file content :
{
"post_filter" : {
"operator" : [
{
"operator" : [
{
"range" : {
"created_at" : {
"gte" : "log_start_date",
"lte" : "log_end_date",
"format" : "yyyy-MM-dd"
}
}
},
{
"terms" : {
"search_param_user_action.id" : "user_select_array"
}
}
]
},
{
"operator" : [
{
"operator" : [
"conduct_days_multi"
]
},
{
"operator" : [
"dropdowns"
]
}
]
},
{
"query" : {
"multi_match" : {
"query" : "x",
"fields" : [
"resource_search_columns"
],
"operator" : "operator"
}
}
}
]
}
}
for any help thanks.
Sometimes some php releasees not supported get_file_contents() or this func return type string but not only return string everytime, sometimes return boolean value bacause it's working on the byte codes, you can try traditional file operation methods or you can try convert to string.
I am having some issues parsing a json file from Jenkins using PHP
{
"actions" : [
{
"causes" : [
{
"shortDescription" : "Started by an SCM change"
}
]
},
{
},
{
},
{
"buildsByBranchName" : {
"origin/release_5.6.0" : {
"buildNumber" : 242,
"buildResult" : null,
"marked" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
},
"revision" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
}
},
"origin/release_5.7.0" : {
"buildNumber" : 315,
"buildResult" : null,
"marked" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
},
"revision" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
}
},
I have tried doing the following
//Read in JSON object
$json_file2 = file_get_contents('url.com/json');
//Decode JSON file
$test = json_decode($json_file2); //object
//print_r($json_file2);
echo $test->causes;
I am also trying to access the different sections in "buildsByBranchName". I have tried many different variations of the code above, but I keep getting "Undefined property: stdClass" errors.
You are not accessing that value properly. causes resides under actions which is an array. Your code also won't work because causes is an array.
// This is an array so you can't use echo here.
$causes = $test->actions[0]->causes;
// echo out the shortDescription
echo $causes[0]->shortDescription;
or
echo $test->actions[0]->causes[0]->shortDescription;
I'm trying to learn json in php. Here's my json result from a ElasticSearch query.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : null,
"hits" : [ {
"_index" : "xenforo",
"_type" : "post",
"_id" : "1816069",
"_score" : null,
"sort" : [ 1365037907 ]
} ]
}
}
I assume that my php code will look something like this:
$myData = json_decode($result);
foreach($myData->hits as $var) {
$post_id[] = $var->_id;
}
Been looking for the answer for hours, I sure do appreciate any help. Thank you.
Edit: Here is the answer:
foreach($myData->hits->hits as $var) {
$post_id[] = $var->_id;
}
You're one ->hits short if you look at your JSON structure...
{
"hits" : {
"hits" : [ {
"_id" : "1816069",
$myData = json_decode($result);
foreach($myData->hits->hits as $hit) {
$post_id[] = $hit->_id;
}
I understand how to parse json with PHP, however I don't understand how to read it with the eye. Can someone please help me understanad this?
Here is my code
<?php
$json = file_get_contents('json.txt');
$json_output = json_decode($json);
foreach ( $json_output->query as $stf )
{
echo "{$stf->response->domains->name}\n";
}
?>
Here is a sample of the json result
{ "query" : { "host" : "test.com",
"tool" : "pro"
},
"response" : { "domain_count" : "13",
"domains" : [ { "last_resolved" : "2012-01-11",
"name" : "test1.com"
},
{ "last_resolved" : "2012-01-11",
"name" : "test2.com"
},
As you can see I tried query->response->domains->name and it didn't work.
How would I tried name?
Thank you in advance
query->response->domains is an indexed array, so you need to get an index, say [0], and then get the ->name from that.
echo $stf->response->domains[0]->name."\n";
foreach ( $json_output->query->response->domains as $domain )
{
echo $domain->name;
}
Study this http://json.org/
If you're trying to read it by eye, it might help to reformat:
{
"query" : {
"host" : "test.com",
"tool" : "pro"
},
"response" : {
"domain_count" : "13",
"domains" : [{
"last_resolved" : "2012-01-11",
"name" : "test1.com"
},{
"last_resolved" : "2012-01-11",
"name" : "test2.com"
}]
}
}