Error in json view invalid json ouput - php

It says invalid json
{"userDetails": "RefId": 565521, {"key":[{"SNO":"565566","Type":"0","Desc":"0"},{"SNO":"12","Type":"1","Desc":"0"}]}, "success":true}
I want to add RefId while including it says invalid json please see and let me know whr m i wrong

You have not written it correctly:
{
"userDetails": [{
"RefId": 565521,
"key": [{"SNO":"565566","Type":"0","Desc":"0"},{"SNO":"12","Type":"1","Desc":"0"}],
"success" : true
}]
}

Your json should be like that :
{"userDetails": {"RefId": 565521, "key":[{"SNO":"565566","Type":"0","Desc":"0"},{"SNO":"12","Type":"1","Desc":"0"}]}, "success":true}
And you should generate it like that :
$resultJson = array(
'userDetails' => array(
'refId' => $reference_id,
'key' => $arr
),
'success' => true
);
echo json_encode($resultJson);

Related

How to prevent json_encode to encode existing json

I have an array like this:
$json = '{ some already encoded json }';
$data = [
'success' => $this->isSuccess(),
'message' => $this->getMessage(),
'statusCode' => (string)$this->getStatusCode(),
'data' => $json
];
in another function I call
json_encode ($data)
The problem is that the json_encode encodes also the already encoded json in the 'data' parameters and escapes it.
How can avoid this ? Is there a straightforward way to do this without changing my whole application ?
I would like to return a json like this:
{ 'success' : true, 'message': 'my message', 'statusCode': 200, 'data': { some already encoded json }}
I'd recommend decoding the json when appending it to data key in the array and later encode everything to json. Like this:
$json = '{ some already encoded json }';
$data = [
'success' => $this->isSuccess(),
'message' => $this->getMessage(),
'statusCode' => (string)$this->getStatusCode(),
'data' => json_decode($json)
];
json_encode($data);

ElasticSearch PHP SDK search returning null on match_all query

I recently tried to use ES. So I set it up in a cloud 9 environnement. I inserted data using a curl request file and I can see them with
http://mydomain/ingredients/aliments/_search?size=350&pretty=true
I then tried to set up elastic SDK (v.2.0) with Silex but I can't get the same output...
Here is my code :
$client = $app['elasticsearch'];
$params = array(
'size' => 350,
'index' => 'ingredients',
'type'=>'aliment',
'body' => array(
'query'=>array(
'match_all' => new \stdClass()
)
)
);
$ingredients = $client->search($params);
The output is NULL but when I do the following :
$params = array(
'index' => 'ingredients',
'type' => 'aliment'
);
$count = $client->count($params);
The output is as expected : {"count":240,"_shards":{"total":5,"successful":5,"failed":0}}
I've already spent a few hours trying to figure what's going on, I tried to replace the 'query' args with a json string, I tried empty array instead of the new stdClass but nothing seems to work.
Edit : I read the documentation again and tried the official example :
$client = $app['elasticsearch'];
$params = [
"search_type" => "scan", // use search_type=scan
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 50, // how many results *per shard* you want back
"index" => "ingredients",
"body" => [
"query" => [
"match_all" => []
]
]
];
$output = $client->search($params);
$scroll_id = $output['_scroll_id']; /*<<<This works****/
while (\true) {
// Execute a Scroll request
$response = $client->scroll([
"scroll_id" => $scroll_id, //...using our previously obtained _scroll_id
"scroll" => "30s" // and the same timeout window
]
);
var_dump($response); /*<<<THIS IS NULL****/
...
}
And unfortunately got same null result...
What am I doing wrong ?
Thanks for reading.
In my case it works this way:
$json = '{
"query": {
"match_all": {}
}
}';
$params = [
'type' => 'my_type',
'body'=> $json
];
I found out that the inserted data was malformed.
Accessing some malformed data via browser URL seems to be OK but not with a curl command line or the SDK.
Instead of {name:"Yaourt",type:"",description:""} , I wrote {"name":"Yaourt","description":""} in my requests file and now everything work as expected !
#ivanesi 's answer works. You may also try this one:
$params["index"] = $indexName;
$params["body"]["query"]["match_all"] = new \stdClass();

How to format json data in php

I have created a webservice and return data in json but on API testing site showing response not in formatted form.
My code is as below :
return json_encode(array("Data" => array("data" => "hello","data2" => "hello1")),JSON_PRETTY_PRINT);
How to get it?
Give a try to
return json_encode($data, JSON_PRETTY_PRINT);
This will give you pretty print of JSON format. Also what you have done should be working fine but what could be wrong is you are missing the JSON header.
header('Content-Type: application/json');
<?php
echo json_encode(
array(
'error' => array(
'error_message' => 'Please choose a date and time',
'error_code' => 500,
)
)
);
?>

How to pass json object using PHP in Wepay API

I have integrated the Wepay payment gateway. But I have facing a problem to pass json object to wepay. It always shows a incorrect json format. Please look at the below code.
$forca_a = array(
'debit_opt_in'=>true
);
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);
$wepay_create_array = array(
'name' =>"xxxx",
'description' => "xxxxxxxxx xxxx",
'callback_uri' => "xxxxxxx",
'country' => "CA",
'currencies' => array('CAD'),
'country_options' => $forca,
'rbits'=> array(
array(
'receive_time'=>strtotime("now"),
'type' =>'website_uri',
'source' => 'partner_database',
'properties'=> array('uri'=>xxxxx)
)
)
);
If I won't pass the country_options, its seems to working but If I pass this parameter, it always give me an error says "Incorrect JSON format".
I sent an email to wepay help center. They told me that, you are passing the string "country_options":"{"debit_opt_in":true}" <--- this is a string Instead of "country_options":{"debit_opt_in":true} <--- this is a JSON object. So I'm confused. I have no idea how do I pass the JSON object. There is only way and is json_encode($object).
hey use below code to get proper json
<?php
$forca_a = array(
'debit_opt_in'=>true
);
// $forca = json_encode($forca_a);
$wepay_create_array = array(
'name' =>"xxxx",
'description' => "xxxxxxxxx xxxx",
'callback_uri' => "xxxxxxx",
'country' => "CA",
'currencies' => array('CAD'),
'country_options' => $forca_a,
'rbits'=> array(
array(
'receive_time'=>strtotime("now"),
'type' =>'website_uri',
'source' => 'partner_database',
'properties'=> array('uri'=>'xxxxx')
)
)
);
print_r(json_encode($wepay_create_array));
?>
this code will give following json output
{
"name": "xxxx",
"description": "xxxxxxxxx xxxx",
"callback_uri": "xxxxxxx",
"country": "CA",
"currencies": ["CAD"],
"country_options": {
"debit_opt_in": true
},
"rbits": [{
"receive_time": 1461561030,
"type": "website_uri",
"source": "partner_database",
"properties": {
"uri": "xxxxx"
}
}]
}
You have no need to make:
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);
before you put it to $wepay_create_array.
Before sending request, i think, you make json_encode($wepay_create_array), and yes, after that you will have 'string' for country_options key.

Write geoJSON to .json file using php

I need to write a new geoJSON feature to a data.json file using php. Right now I'm writing my data to the file like so:
<?php
// Read from json file
$jsondata = json_decode( file_get_contents('data.json') );
// Add the new data
$jsondata []= array(
'measure_location'=> $_POST["measure_location"],
'measure_type'=> $_POST["measure_type"],
'measurement'=> $_POST["measurement"],
'note_text'=> $_POST["note_text"]
);
// encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
$jsondata = json_encode($jsondata, JSON_PRETTY_PRINT);
// saves the json string in "data.json" (in "dirdata" folder)
// outputs error message if data cannot be saved
if(file_put_contents('data.json', $jsondata));
?>
And this is what the data then looks like in data.json:
{
"measure_location": "52.370611247493486, 4.91587221622467",
"measure_type": "negative",
"measurement": "violence",
"note_text": ""
}
Could I adjust my PHP code to make the data look like so:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
"4.91587221622467",
"52.370611247493486"
]
},
"properties": {
"type": "negative",
"input": "violence",
"note": ""
}
}
Got the answer thanks to charlietfl. Changed the php code to:
<?php
// Read from json file
$jsondata = json_decode( file_get_contents('data.json') );
// Add the new data
$jsondata [] = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => $_POST["measure_location"],
),
'properties' => array(
'type' => $_POST["measure_type"],
'input' => $_POST["measurement"],
'note' => $_POST["note_text"],
)
);
// encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
$jsondata = json_encode($jsondata, JSON_PRETTY_PRINT);
// saves the json string in "data.json" (in "dirdata" folder)
// outputs error message if data cannot be saved
if(file_put_contents('data.json', $jsondata));
?>
I cannot the code that works for you. I have tried many times. It produced a wrong format JSON. The correct code should be,
$jsondata -> features [] = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => $_POST["measure_location"],
),
'properties' => array(
'type' => $_POST["measure_type"],
'input' => $_POST["measurement"],
'note' => $_POST["note_text"],
)
);

Categories