CakePHP jSon format change - json

I'm finding list of Areas for my CakePHP 2.x website and it supports, JSON output as below with find all method:
$this->Area->find('all', array('fields' => array('id', 'name', 'order'), 'conditions' => array('Area.status' => 1)));
Below is my JSON output:
[{"Area":{"id":"2","name":"Area 1","order":"1"}},{"Area":{"id":"3","name":"Area 2","order":"1"}}]
Now Is that possible for me to remove Area tag which is repeating everytime?
Any patch for same? Do let me know if any suggestions / ideas.

on your view for output json write this:
echo json_encode(Set::extract('/Area/.', $area));
For me works fine.

CakePHP Provides some in-built library functions for data extraction from result set and output same as JSON format.
// initialize your function to render false and response type json
$this->autoRender = false; // no view to render
$this->response->type('json');
// Remove Area from array and encode
$area = Set::extract('/Area/.', $area);
$json = json_encode( $area);
$this->response->body($json);
Hope it helps !

Related

Using Mojo::UserAgent and accessing the JSON in response?

How can I get access to JSON in a mojo response?
$txn = $ua->post( $url, $headers, json => {json} )
What's the way to get the JSON response from the txn?
I have several examples in my book Mojolicious Web Clients, but here's the deal.
When you make a request, you get back a transaction object:
my $ua = Mojo::UserAgent->new;
my $tx = $ua->post( ... );
The transaction object has both the request and response (a major feature that distinguishes Mojo from LWP and even other user agent libraries in other languages). To get the response, you can use the res or result methods. The result dies for you if it couldn't make the request because a connection error occurred (ENONETWORK):
my $res = $tx->result;
Once you have the response, there are various things you can do (and these are in the SYNOPIS section of the Mojo::UserAgent. If you want to save the result to a file, that's easy:
$res->save_to( 'some.json' );
You can turn the content into a DOM and extract parts of HTML or XML:
my #links = $res->dom->find( 'a' )->map( attr => 'href' )->each;
For a JSON response, you can extract the contents into a Perl data structure:
my $data_structure = $res->json;
However, if you wanted the raw JSON (the raw, undecoded content body), that's the message body of the request. Think of that like the literal, unfiltered text:
use Mojo::JSON qw(decode_json);
my $raw = $res->body;
my $data_strcuture = decode_json( $raw );
Since this is the response object, Mojo::Message and Mojo::Message::Response show you what you can do.
Here's a complete test program:
#!perl
use v5.12;
use warnings;
use utf8;
use Mojo::JSON qw(decode_json);
use Mojo::UserAgent;
use Mojo::Util qw(dumper);
my $ua = Mojo::UserAgent->new;
my $tx = $ua->get(
'http://httpbin.org/get',
form => {
name => 'My résumé'
},
);
die "Unsuccessful request"
unless eval { $tx->result->is_success };
my $data_structure = $tx->res->json;
say dumper( $data_structure );
my $raw = $tx->res->body;
say $raw;
my $decoded = decode_json( $raw );
say dumper( $decoded );
I was able to get access to this data like this,
my $api_order = $tx_cart->result->json->{data};
It's in result not in body.

Issues trying to access an associative array converted from JSON in Laravel

I'm working on a Laravel project that implements react-jsonschema-form and I need to convert the values saved in the database to an associative array so I can pluck certain values from it. However I am getting strange results when doing so.
Here is the code I use to grab the JSON data from the table and then convert to an array:
$form = Form::where('id', $formId)->get();
$converted = json_decode($form[0]->form_data, true);
$formArray = print_r($converted, 1);
return $formArray;
For testing purposes I am simply rendering the data in the browser.
The result from the above return is:
Array
(
[1] => Array
(
[1.1] => New
[1.2] => Ms
[1.3] => Isobel Fleming
[1.4] => Array
(
[uprn] => 52375918
[address_1] => Fake Street
[address_2] =>
[address_3] =>
[town_city] => BRISTOL
[postcode] => BS1 3KE
)
[1.5] => 0129711011
[1.6] => 0800999111
[1.7] => 0781100022
[1.8] => isobelfleming#jourrapide.com
)
)
which is great. However when I try and access anything from it like:
return $formData[1][1.1]
I get:
String offset cast occurred
If I try using a string:
return $formData[1]['1.1']
I get:
Illegal string offset '1.1'
So I am not sure what to do to access this data. The problem is, although it's not ideal to have the associate keys with decimals in them, this is the way the schema is set up and it's several thousand lines long - this is just a snippet of the form data.
Is there anything that can be done in order to get the data from this array?
I figured it out. It seems like the following line was the problem:
print_r($converted, 1);
It didn't need to be printed as an array, I should have just used the $converted variable to access the data like so:
return $converted[1]['1.1'];

Get variable GET in Yii2 not working

Basiclly I have an url that have a parameter.
So, I need to access those parameter using GET. This is the GET that coming:
Array
(
[r] => hanwa/incoming-pipe/assign-incoming
[1] => Array
(
[min_urut] => 1
[max_urut] => 44
)
[_] => 1496645704980
)
In docs, docs,
We can use like this :
echo $request->get('min_urut');
But, I got nothing. Please Advise.
Use Concept of ArrayHelper class :-> http://www.yiiframework.com/doc-2.0/guide-helper-array.html#getting-values
ex:
$data = ArrayHelper::getValue($request->get(), 'Temp.yourvalue.yourindex');
Main use of $request->get() method mainly returns to you a value from $_GET,
so example is
$temp = $request->get('Temp'); // Here $temp variable contains $_GET['Temp']
$data = $temp['yourvaluename'][0];
Judging by the fact that you have an array with the key [r] you have an associative array, method get() you will not get it, you need to do it just like this ..
echo $request->get()[1]['min_urut'];

Symfony2 - ChoiceType - get choice list from JSON without JS

I have a JSON file with all world languages and would like to put them into choices array inside ChoiceType form field.
$builder->add('languages', ChoiceType::class, array(
'choices' => array()
))
My JSON file is stored: projectname/web/bundles/index/json/languages.json
Is it possible to achieve it without writing JS / AJAX?
P.S. EventListeners or other alternatives that Symfony2 provides suits me well.
You could reach the file with DIR, since I dont' know where the php file with builder is located, it could looks like:
$builder->add('languages', ChoiceType::class, array(
'choices' => json_decode(
//if builder is in controller, this should work
file_get_contents(__DIR__.'/../../../web/bundles/index/json/languages.json'),
true)
));

Cakephp 2.5 Order by query

i am trying to apply order by query on my publication table and then applying json encode, its works fine before json encode. and even after json encode in window, but on live server json encoding is not working, can anyone plz guide me where i am doing mistake.
the code is here below
public function articles($offset = null) {
$order=array('Publication.id' =>'DESC');
$total2 = $this->Publication->find('all', array('limit' => 10, 'offset' => $offset * 10, 'order' => $order));
echo json_encode($total2); die();
}