How to format json data in php - 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,
)
)
);
?>

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);

json_encode doesn't output to echo correctly - CodeIgniter 3, PHP 7.4 + NGINX, Ubuntu 18

I have a strange problem. There is an Ajax request (POST), which sends data to the CodeIgniter 3 endpoint and everything is ok. The data is send and I can access it there, but in the PHP, when I try something like:
echo json_encode([
'success' => false,
'message' => 'Not found'
]);
- empty response
The only way to output this is to 'echo' something BEFORE that like this:
echo 1;
echo json_encode([
'success' => false,
'message' => 'Not found'
]);
1{"success":false,"message":"Not found"}
It can be a char or a bool true (which is 1 in the browser response) and it must be before json_encode.
I have tried to set up headers to the response json or html, but with no effect.
It is the same and with Postman.
Edit.
This works, too:
echo json_encode('test');
, but I need a collection.
return json_encode([
'success' => false,
'message' => 'Not found'
]);

Shapeshift API always returns "No Withdrawal Address Specified" error

I am using Shape Shift API for sending transaction with Guzzle. I am always getting the error given in title. My code given below:
$client = new GuzzleHttp\Client();
$data = [
"amount" => 1,
"withdrawal" => '0x23f016d7a8e408e5551ae7aa51b3fe1534165463',
"pair" => 'btc_eth',
"returnAddress" => '12stJs8vZNuuVfjZSSzpLPA96quNissk1b'
];
$result = $client->post( 'https://shapeshift.io/shift', [ 'Content-Type' => 'application/json' ],
[ 'json' => $data ] );
print "<pre>";
print_r( $result->getBody()->getContents() );
print "</pre>";
Same parameters work fine when using in Python.
You are doing the request wrong. The correct variant is with two parameters:
$result = $client->post('https://shapeshift.io/shift', ['json' => $data]);
Content type is configured automatically when you use json option.

Error in json view invalid json ouput

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);

API format in PHP

I'm working with API of a delivery service. They provide an opportunity to test the API on their website, where the following code works perfectly:
{
"modelName": "InternetDocument",
"calledMethod": "documentsTracking",
"methodProperties": {
"Documents": [
"20450000773554",
"20450000773555"
]
}
}
But when I try to convert it to PHP format, I don't know what to do with the arrays of 'methodProperties' in PHP syntax.
$data = array(
'modelName' => 'InternetDocument',
'calledMethod' => 'documentsTracking',
'methodProperties' => ???
);
Can you please help me to convert the code to PHP correctly?
You need to understand how json_encode and json_decode functions work:
This as JSON string:
{
"modelName": "InternetDocument",
"calledMethod": "documentsTracking",
"methodProperties": {
"Documents": [
"20450000773554",
"20450000773555"
]
}
}
is equivalent to this in PHP:
array(
'modelName' => 'InternetDocument',
'calledMethod' => 'documentsTracking',
'methodProperties' => array(
'Documents' => array(
"20450000773554",
"20450000773555"
)
)
);
Try that
<?php
$data = array(
'modelName' => 'InternetDocument',
'calledMethod' => 'documentsTracking',
'methodProperties' => array('Documents' => array('20450000773554', '20450000773554'))
);
echo json_encode($data);
?>
JSON :
$json = {
"modelName": "InternetDocument",
"calledMethod": "documentsTracking",
"methodProperties": {
"Documents": [
"20450000773554",
"20450000773555"
]
}
}
the data of this format is known as JSON(Javascript object notation),So,If you want to access it by using PHP you have to decode it using json_decode predefined function in PHP.
PHP:
$decoded_string = json_decode($json);
now use,
echo "<pre>";
print_r($decoded_string);
you get the following data in the form of array and you can easily parse this data in your application.
array(
'modelName' => 'InternetDocument',
'calledMethod' => 'documentsTracking',
'methodProperties' => array(
'Documents' => array(
"20450000773554",
"20450000773555"
)
)
);

Categories