Drupal 7 Services JSON shows fields names with spaces - json

I have drupal 7 deployment with services 3 module. I have Services with JSON output configured. When I get my results, the custom fields return labels instead of the actual field names. For example, Node Title which is built in shows node_title. However, 1 Year a custom field that was stored as field_1_year shows up as 1 Year. This makes it difficult to parse JSON. Any suggestions?

You can make your custom json feed i.e.:
Make you php script and at top add standard D7 bootstrap:
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
After this code you'll have all Drupal's functionalities available in your script.
Add your code to get values you want. You can use Drupal's database api, or even easier, create some view and use views_get_view_result() function to get values view returns:
https://api.drupal.org/api/views/views.module/function/views_get_view_result/7
Then iterate trough your results and create another php array containing values you want in the way you want.
Use json_encode to convert your array to json string:
http://php.net/manual/en/function.json-encode.php
Print out your json string. You can even print out json header before it, so apps that are getting feed will know it's json (sometime may be needed)
header('Content-Type: application/json');
Something like that...

Related

Adding query Parameters to Go Json Rest

I am using the library go-json-rest. I'm trying to recognize queries parameters in the code for example localhost:8080/reminders?hello=world I want to access {hello: world} . I have the following code:
//in another function
&rest.Route{"GET", "/reminders", i.GetAllReminders},
func (i *Impl) GetAllReminders(w rest.ResponseWriter, r *rest.Request) {
reminders := []Reminder{}
i.DB.Find(&reminders)
w.WriteJson(&reminders)
}
I know that r.PathParams holds the url parameters but I cannot seem to find how to the query parameters past the "?" in the url.
Given that go-json-rest is a thin wrapper on top of net/http, have you looked at that package's documentation? Specifically, the Request object has a field Form that contains a parsed map of query string values as well as POST data, that you can access as a url.Values (map[string][]string), or retrieve one in particular from FormValue.

How to insert json encoded inputs into foreign related tables

I am creating functionalities in yii+extjs. My client side is desighned in extjs and server side is in yii framework.
I am having two tables as
Poll Option
-pollId -optionId
-PollQuestion -option
-pollId
Now through poll creation form which will be in extjs,Question and its related option gets send to server in json format. So in yii framework,in actionCreate function will get input as-
$json='{"pollId":1,"PollQuestion":"Who is the best
cricketer","option":"ABC","option":"DEF","option":"XYZ"}'
$obj=json_decode($json);
During creation of poll,user can enter any number of options.So number of options can be anything.
i am creating above functionality in Pollcontroller.
So this newly created question gets inserted into Poll table as=
$model=new Poll();
$model->pollId=$obj->pollId;
$model->PollQuestion=$obj->PollQuestion;
Now i want to put all these new options in option table with same pollId. So how to add all these options in option table? Please help me
I would start by modifying the JSON so the options are a separate JSON within the pollquestion JSON.
Something like this...
$json='{"pollId": 1,"PollQuestion": "Who is the best cricketer",
"options":{[{"value":"ABC"},{"name": "DEF"},{"name": "XYZ"}]}';
That way when you decode it with json_decode you'll get an options array which you can go through and add each of the elements in that array in the options array.
for($i=0; $i<sizeof($obj['options']);$i++){
//Add to table logic here
}

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.

How to query a Lotus view and have the result formatted as json?

I have a view called 'walking' which I want to query:
http://site/activity.nsf/walking?searchview&query=FIELD%20Gradient%20CONTAINS%20gradienteasy.gif
This returns the results in an HTML table. What I would like to do is have the results formatted as JSON which I will then use client-side. Is this possible?
I know you can get JSON returned from a straight view by doing this:
http://site/activity.nsf/walking?readviewentries&outputformat=json
Scott Good and I have done several sessions at a variety of conferences on generating and consuming JSON from traditional Domino applications (not using XPages). The most recent was the "JMP303 JSON in client- and server-side code Master Class" we gave at Lotusphere 2011. Link to the presentation materials and slides are: here
/Newbs
You will have to create a view that is marked with the "Treat view contents as HTML" property and set up a column formula that generates the JSON syntax that you want'.
There is a nice post on OpenNTF with the code to create a very generic view which returns JSON for the documents that match the View's selection formula:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=use-transform-to-build-json-and-consume-the-output-in-an-xagent
That sample uses an "XAgent" (Xpage with no UI) to set the content-type header, etc. But you could probably do the same thing using a $$ViewTemplate form, if needed.