This is my PHP code for json output:
$sql="SELECT id,name FROM languages ORDER BY id";
$result=mysqli_query($conn,$sql);
// Fetch all
$result = mysqli_fetch_all($result,MYSQLI_ASSOC);
$out_put = json_encode($result);
echo $out_put;
This is the json output of the above php code:
{
"0": {
"id": "1",
"name": "English"
},
"1": {
"id": "2",
"name": "Kanada"
},
"2": {
"id": "3",
"name": "Hindi"
},
"3": {
"id": "4",
"name": "Telugu"
}
}
But I want output like this:
{
"Responsecode":200,
"Message":"Sucess",
"languagelist": [
{
"id": "1",
"name": "English"
},
{
"id": "2",
"name": "Kannada"
},
{
"id": "3",
"name": "Hindi"
},
{
"id": "4",
"name": "Telugu"
}
]
}
I am trying to create API and I am new in it. Please help. Thank you in advance.
Just write
$out_put = json_encode([
"Responsecode" => 200,
"Message" => "Sucess",
"languagelist" => $result
]);
You can do it by this way:
$output['Responsecode'] = 200;
$output['Message'] = "Sucess";
$output['languagelist'] = $result;
$out_put = json_encode($output);
Related
I can't properly use INNER JOIN. This is the actual return output via json on php.
[
{
"id": "1",
"itinerary_customer_id": "856",
"datetime_created": "2020-04-20 19:54:16",
"documents": [
{
"id": "1",
"itinerary_id": "1",
"datetime_created": "2020-04-20 22:51:12"
},
{
"id": "2",
"itinerary_id": "1",
"datetime_created": "2020-04-20 22:51:12"
},
]
},
{
"id": "2",
"itinerary_customer_id": "857",
"datetime_created": "2020-04-20 19:54:16",
"documents": [
{
"id": "3",
"itinerary_id": "2",
"datetime_created": "2020-04-20 22:51:12"
},
{
"id": "4",
"itinerary_id": "2",
"datetime_created": "2020-04-20 22:51:12"
},
]
}
]
Its look like array json and there's possible array inside each data.
Here's my existing code.
$sql = "SELECT * FROM itinerary INNER JOIN itinerary_documents ON itinerary_documents.itinerary_id = itinerary.id WHERE itinerary_customer_id = '".$customer_id."' ";
$result = $mysql->query($sql);
if ($result->num_rows > 0) {
while ($row[] = $result->fetch_assoc()) {
$item = $row;
$json = json_encode($item);
}
}else{
$item = array("error"=> true, "message"=> "No Itinerary found.");
$json = json_encode($item);
}
echo $json;
In my JsonArray result, I want to put all the content in index [{'data':{contenthere}}]
Here is my code :
$data = [];
$gr = []; //some data here
foreach($gr as $g) {
$data[] = [
'id' => $g['id'],
'name' => $g['name'],
'phone' => $g['pĥone']
]
}
return $data;
return $data output :
string(249) "[
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
},
]
cool, now I want to put all this in ['data'] so the result needed :
string(249) "[
{
"data": {
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
},
}
}
]
As Google JSON Style
I tried the $data[]['data'] but the "data": repeats in each object
foreach($gr as $g) {
$data[]['data'] = ...
I thought do do group_by function but I think that there is a simple solution for that
I'm not sure if you really need the extra array at the top level, but use...
return [["data" => $data]];
should give you the result.
Use json_encode to achieve this:
$json = json_encode(array('data' => $data));
print_r($json);
Result:
{
"data": [
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
}
]
}
I have a json data generated with the following php code
$index = array();
foreach($rows as $row)
{
$row['data'] []= (object) [];
$index[$row['cat_id']] = $row;
}
// build the tree
foreach($index as $id => &$row)
{
if ($id === 0) continue;
$parent = $row['parent'];
$index [$parent]['children'][] = &$row;
}
unset($row);
// obtain root node
$index = $index[0]['children'][0];
$json_data = json_encode($index, JSON_PRETTY_PRINT);
which produces the following json
{
"cat_id": "1",
"id": "RT",
"name": "root",
"parent": "0",
"url": "www.test.com",
"data": [
{}
],
"children": [
{
"cat_id": "4",
"id": "CI.001",
"name": "Test2",
"parent": "2",
"url": "www.test.com",
"data": [
{}
]
}
what i want is the output to be like this
var something = [{
"cat_id": "1",
"id": "RT",
"name": "root",
"parent": "0",
"url": "www.test.com",
"data": [
{}
],
"children": [
{
"cat_id": "4",
"id": "CI.001",
"name": "Test2",
"parent": "2",
"url": "www.test.com",
"data": [
{}
]
}];
I have looked through the web with some suggesting that it is not json but php array i'm new to json. How can I solve this ?
Here is is my solution at the moment.
ob_start();
echo "var javascript_array = [". $json_data . "];\n";
$content = ob_get_contents();
file_put_contents('../core/json/tree.js', $content);
Any suggestions and improvements will be appreciated.
I am building an admin backend for an android app. My code provides a json output which the android developer then uses in his app. The developer asked me if it is possible to reduce the hierarchy level in the json output
as in remove the highlighted []0 and put the contents directly inside the []data instead.
This is my current php code
if($type == 1 ) //Handle item display
{
try
{
$query = "SELECT category FROM category";
$result= $DBH->query($query);
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$cat = $row['category'];
$query1 = "SELECT * FROM item WHERE catagory='$cat'";
$value = $DBH->query($query1);
if($row1 = $value->fetchAll(PDO::FETCH_OBJ)){
$main[] = array('data'=>array($row1));
}
else
{
$main[] = array('data'=>array('catagory'=>$row['category']));
}
}
echo json_encode($main);
$result->closeCursor(); //Close database connection free resources
$DBH = null;
}
catch(PDOException $e){
print $e->getMessage ();
die();
}
}
And the json output being generated
[
{
"data": [
[
{
"id": "2",
"name": "rice",
"price": "20",
"description": "Plain Rice",
"image": "4106_rice.jpg",
"time": "12 mins",
"catagory": "Lunch",
"subcat": ""
},
{
"id": "3",
"name": "item 1",
"price": "32",
"description": "item 1 description",
"image": "1370_2Ckjikljklkljh.jpg",
"time": "10",
"catagory": "Lunch",
"subcat": "Chicken Soup"
},
{
"id": "4",
"name": "hello",
"price": "10",
"description": "fgsdjfsfsdj",
"image": "",
"time": "76",
"catagory": "Lunch",
"subcat": ""
}
]
]
},
{
"data": {
"catagory": "Dinner"
}
},
{
"data": {
"catagory": "Soup"
}
},
{
"data": {
"catagory": "Test"
}
}
]
I am not very sure if I can make the changes he asked or if it is possible. Is it doable?
Your json structure is inconsistent and try this approach, will be easier for the developer to parse
{
"response": [
{
"data": [
{
"id": "2",
"name": "rice",
"price": "20",
"description": "Plain Rice",
"image": "4106_rice.jpg",
"time": "12 mins",
"catagory": "Lunch",
"subcat": ""
},
{
"id": "3",
"name": "item 1",
"price": "32",
"description": "item 1 description",
"image": "1370_2Ckjikljklkljh.jpg",
"time": "10",
"catagory": "Lunch",
"subcat": "Chicken Soup"
},
{
"id": "4",
"name": "hello",
"price": "10",
"description": "fgsdjfsfsdj",
"image": "",
"time": "76",
"catagory": "Lunch",
"subcat": ""
}
]
},
{
"data": [
{
"catagory": "Dinner"
}
]
},
{
"data": [
{
"catagory": "Soup"
}
]
},
{
"data": [
{
"catagory": "Test"
}
]
}
]
}
I have a very simple JSON file "json.txt" :
{
"status": true,
"data":
{
"clics":
[
{
"id": "1",
"title": "Title 1",
"comment": "Blablabla 1",
"url": "http://photostodisplay/1.jpg"
},
{
"id": "2",
"Title": "Title 2",
"comment": "Blablabla 2",
"url": "http://photostodisplay/2.jpg"
}
]
}
}
and I would like to add data and get the following result :
{
"status": true,
"data":
{
"clics":
[
{
"id": "1",
"title": "Title 1",
"comment": "Blablabla 1",
"url": "http://photostodisplay/1.jpg"
},
{
"id": "2",
"Title": "Title 2",
"comment": "Blablabla 2",
"url": "http://photostodisplay/2.jpg"
},
{
"id": "3",
"Title": "Title 3",
"comment": "Blablabla 3",
"url": "http://photostodisplay/3.jpg"
}
]
}
}
The php code I use return the following error :
Fatal error: Cannot use object of type stdClass as array in /home/XXXX/www/clic/test.php on line 10
Here is the php code
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$file = 'json.txt';
$data = json_decode(file_get_contents($file));
$newdata = array('id'=>'11', 'title' => 'sfdfsdfqf', 'comment' => 'sfdfwwfwdsdfqf', 'url' => 'sdfqwsfsdqqfqqqsfcq');
$data[] = $newdata;
file_put_contents($file, json_encode($data));
echo OK
?>
EDIT - Cezary answer gives almost what I need but is not. Here is what I get :
{
"status": true,
"data": {
"clics": [
{
"id": "1",
"title": "Title 1",
"comment": "Blablabla 1",
"url": "http://photostodisplay/1.jpg"
},
{
"id": "2",
"Title": "Title 2",
"comment": "Blablabla 2",
"url": "http://photostodisplay/2.jpg"
}
]
},
"0": {
"id": "11",
"title": "sfdfsdfqf",
"comment": "sfdfwwfwdsdfqf",
"url": "sdfqwsfsdqqfqqqsfcq"
}
}
The issue lies with this bit of code:
$data = json_decode(file_get_contents($file));
This returns an object, not an associative array. Change that line to this:
$data = json_decode(file_get_contents($file), true);
The second argument in json_decode specifies whether you want an associative array or not, and it defaults to false.
EDIT:
You're currently adding something to the root array. To add to the clics array, you can replace $data[] = $newdata; with:
$data['data']['clics'][] = $newdata;
Shouldn't that be
$data["data"]["clics"][]=$newdata;
or
array_push($data["data"]["clics"],$newdata);