Save api json to database using laravel - json

Is it possible to get data from an api url and save it directly to database when working with laravel? the data i get from the url is of the format {"name":"100KVA SUKAM Generator","level":"5.965"}.

Yes, you can create table with json type field (or text) and keep data there:
$table->json('data_from_api');
https://laravel.com/docs/5.2/migrations#writing-migrations
If you want to persist data as usual data, you can use mass assignment. First, convert JSON to an array with [json_decode][1] and save data like that:
$data = json_decode($jsonData, true)
Model::create($data);
Don't forget to add all columns to a $fillable property of a model.

Related

Laravel generate forms with JSON

Is there a way in Laravel to save JSON code in one table field and when you load that JSON code you get generated form (view) where you can submit data that can be saved in JSON code and then again saved in one table field?
you can use Redis.
$formData = Redis::set('formdata',$json);
$formData = Redis::get('formdata');

how to convert to json from post in Yii2

I want to create json from multiple selection from listbox in yii2 after post i receive following data
categorymodel[0]=11&categorymodel[1]=12&categorymodel[2]=13
how to convert to json data and store in db.
json data like {"11","12","13"}
Once you have the proper array you can use eg:
$retJSON= json_encode($categoryModel);

JSON data in spring model

Its a spring based web application. I have a data table in the screen. In my understanding, data table accepts only data in JSON format. so i have a field(String) in my model for holding data table data, the same will be hidden in the screen. Upon listing(form submit not ajax), I would serialize the list to JSON data and keep it in the model. In Javascript, I would populate the data table using the data in the hidden field. It works fine but i have a doubt like whether it is a correct approach ?
I have a export to excel functionality. I would pass the model in the ajax call and get the data from the model and create the XLSX file. Issue is if the data has 3000 records, it works fine but when the record get increases say 4000 the data in the model is coming as null in the handler. Is there any data size limit for the Spring to bind the value to model ?
To your first question, the approach which you are following is correct.
For Export Excel functionality, you can pass the same JSON string which you stored in a hidden variable. And DE-serialize the JSON string to the data. Since JSON is just a string, there is no such limit on Ajax form data

In yii how json formatted inputs are inserted into tables

I am creating project using extjs and yii. My client side design is in extjs-4 and server side design is in yii framework.
Now I am having table Poll with fields as:
pollid
pollQuestion
Isactive
Userid
And Polloption:
pollid
option
Now during creation of new pole,poll creation view form which is designed in extjs will receive inputs and will send this data to server side in json format as-
{
'success':true,
'results':[ {
'pollid' : 1,
'pollQuestion' : 'Which is capital of india',
}
{ options from polloption table in json format
}]
}
So at server side all this values will come in json format. So now in yii i want to insert this received inputs in corresponding poll tables fields.
So how Yii will convert this json formatted inputs and also insert those values into repective fields of poll table. Please help me.
Your question is very vague and general. Here's an overview of what you'll want to do:
submit the data to a Yii controller
If the data is in the body, use PHP's file_get_contents. If you POST or GET it, you can use Yii's CHttpRequest::getParam to read in the raw JSON
use CJSON::decode() to parse the JSON into a PHP array
manipulate the array values, and build a new array
return the data (echo or print it if you just need the raw JSON and don't need a view). You'll probably want to return JSON again to use it in extjs4, so you'll want to use the CJSON::encode() method

ServiceNow - JSON Web Service, display related tables

I'm working on a C# program that retrieves data from a ServiceNow database and converts that data into C# .NET objects. I'm using the JSON Web Service to return my data in JSON format.
What I want to achieve is as follows: If there is a relational mapping between a value (for
example: I have a table called Company, where CEO is not a TEXT field but an sys_id to a Employee Table) I want to be able to output that data not with an sys_id (or just displaying the name property by using the 'displayvariable' parameter) but by an object displayed in JSON.
This means that the value of a property should be an object in JSON instead of just a single value.
A few examples:
// I don't want the JSON like this
{"Company":{"CEO":"b181e841c9212c008aeb36850331fab2"}}
// Or by displaying the name of the sys_id table
{"Company":{"CEO":"James Henderson" }}
// I want the data as follows, so I can have all the data I need inside a single JSON record.
{"Company":{"CEO":{"name":"James Henderson", "age":34, "sex":"male", "office":"SBN Left Floor 23"}}}
From reading the documentation I couldn't find anything in the JSON Web Service that allowed me to display the information like this nor
find any other alternative. It should have something to do with joining the tables and displaying it all in the right format.
I have been using SNC for almost three years and have not found you can automatically join tables in a web service. Your best option would be to use a scripted web service which possibly takes a query parameter and table parameter. Then you can json serialized your result as you see fit.
Or, another option would be to generate a new processor that will traverse the GlideRecord object. The ?JSON parameter you pass in to the URL is merely a flag to pass your request to a particular processor. Unfortunately the OOB one I believe is a Java class not a JS script, so you would need to write a script much like I mentioned earlier to traverse the object path serializing the object graph as far down as your want to go.