ibm worklight stored procedure - json

I want to parse this json object in javascript and have to obtain the values of key pass in string format.
{
"isSuccessful": true,
"resultSet": [
{
"name": "a",
"pass": "123",
"time_stamp": "2014-04-07T10:13:17.000Z"
},
{
"name": "chetan",
"pass": "123456",
"time_stamp": "2014-04-07T10:13:34.000Z"
},
{
"name": "dileep",
"pass": "456321",
"time_stamp": "2014-04-07T10:13:54.000Z"
},
{
"name": "bnc",
"pass": "654321",
"time_stamp": "2014-04-07T10:19:37.000Z"
}
]
}

If you are asking in a general sense (no links to Worklight) I would look at other answers:
How do I iterate over a JSON structure?
JavaScript loop through json array?
How do I loop through or enumerate a JavaScript object?
In Worklight, if your example is the response of a Worklight adapter, the response will be in a property called invocationResult.
See also https://www.ibm.com/developerworks/community/blogs/worklight/entry/handling_backend_responses_in_adapters?lang=en

Related

How to add root node in JSON for every object?

I convert excel file to JSON , to import it into my firebase DB.
On conversion, I have the JSON data in below format
[
{
"ProductNumber": "7381581",
"SKU": "test3",
},
{
"ProductNumber": "7381582",
"SKU": "test",
},
{..}
]
But I need it like this
{
"7381581" :{
"ProductNumber": "7381581",
"SKU": "test3",
},
"7381582":{
"ProductNumber": "7381582",
"SKU": "test",
},{..}
}
How can I make changes to the spreadsheet records to get the JSON in the above format ? (OR)
How should I add the key values to JSON dynamically?
You can use reduce as suggested to iterate over the original array and transform it into an object.
data.reduce((prev, current) => {
prev[current.ProductNumber] = current;
return prev;
}, {});
You can see a working example in the playground here.

How to fetch inner node of JSON using freemarker?

Sample JSON:
{
"results": [
{
"_id": "12345",
"CustomerAccount": {
"Status": "ACTIVE",
"Address": [
{
"FormatCode": "PRIM",
"FreeFormatAddress": "ENGLAND"
},
{
"FormatCode": "SEC",
"FreeFormatAddress": "IRELAND"
}
]
},
"LegalVehicleID": "01",
"BrokerAccount": {
"Status": "ACTIVE",
"Address": [
{
"FormatCode": "PRIM123",
"FreeFormatAddress": "SG"
},
{
"FormatCode": "SEC123",
"FreeFormatAddress": "IND"
}
]
}
}
]
}
So i want to get Address as list regardless of CustomerAccount or BrokerAccount.
result.*.Address and then access FormatCode and FreeFormatAddress using FreeMarker.
Please let me know how to do so thanks.
There's no such feature in the template language itself. Such things can be achieved with a TemplateModel that was designed to do queries against JSON, similarly as such queries are possible against XML (W3C DOM to be more precise) out-of-the-box. But I'm not aware of anybody has written such thing for JSON (and even then it's question what Java objects represent the JSON... like, is it Jackson JsonNode-s?).

Sending the JSON response as array or normal object

I am implementing a restful service where I am getting the pdf names and their ids from the database in the JSON fromat. Which one of the both the convenient JSON resful service response?
First Option:
{
"results": {
"documentNames": [
"test.pdf",
"ireport-ultimate-guide.pdf",
"sending report.pdf",
"Motor Hour.pdf"
],
"documentds": [
21116,
21117,
21118,
21119
]
}
}
Second Option:
{
"results": {
"21116": "test.pdf",
"21117": "ireport-ultimate-guide.pdf",
"21118": "sending report.pdf",
"21119": "Motor Hour.pdf"
}
}
I would use this "third option": The result is a list of object.
{
"result": [{
"id": "21116",
"filename": "test.pdf"
},
{
"id": "21117",
"filename": "ireport-ultimate-guide.pdf"
},
{
"id": "21118",
"filename": "sending report.pdf"
},
{
"id": "21119",
"filename": "Motor Hour.pdf"
}
]
}
because it better models the object structure.
I would create an entity for each document that contains both name and I'd.
[
{"name": "doc_1", "id": 123},
{"name": "doc_2", "id": 456}
]

XML response to an express API

I have a node js program, which uses the express framework. What happens, is a POST request is made using Postman to my API, and I deal with the request as required (which works great).
However, I want send back an XML response to the API call. So doing some digging online, I have found this library - https://www.npmjs.com/package/xml
I tried to adapt it to my code, so I need to convert the following json object into an XML response:
var responseJson = [{
"methodResponse": {
"params": {
"param": {
"value": {
"struct": {
"member": [
{
"name": "myValue",
"value": {
"string": "hi"
}
}
]
}
}
}
}
}
}];
And then in the response I do the following:
res.header('Content-Type', 'text/xml');
res.send(xml(responseXml, true));
However this only returns:
<methodResponse/>
and nothing else in the Postman response.
Any idea what happened to the rest and why only one line is returned? Is there a better way to do this? Thanks
You need to put square brackets around your objects.
const data = [{
"methodResponse": [{
"params": [{
"param": [{
"value": [{
"struct": [{
"member": [{
"name": "myValue",
},{
"value": [{
"string": "hi"
}]
}]
}]
}]
}]
}]
}]
}];
Which will produce:
<methodResponse><params><param><value><struct><member><name>myValue</name><value><string>hi</string></value></member></struct></value></param></params></methodResponse>

How to Force Jersey to generate Specified JSON Response

So, I have started using Jersey as the implementation of JAX-RS in our application for REST services and I came across a strange issue with it. We have been provided with a standard response as below:
"Outer":
{
"agencyPercentagePayment": "80",
"agencyProviderPaymentAmount": "140.00",
"benCoDtls":
{
"benCode": "String1",
"fullName": "String2",
"id": "String3",
"title": "String4"
},
"levelOfCare": "Full-Time",
"paymentDate": "2014-02-19T15:20",
"titleInfo":
{
"benAmt": "140.0",
"benDesc": "AAAA",
"subTitle": "aaaa",
"title": "String"
},
"units": "10"
}
However, The JSON generated by the Jersey provider generates an arbitary response as below:
"Outer":
{
"agencyProviderPaymentAmount": "140.00",
"benCoDtls":
{
"benCode": "String1",
"fullName": "String2",
"id": "String3",
"title": "String4"
},
"agencyPercentagePayment": "80",
"titleInfo":
{
"benAmt": "140.0",
"benDesc": "AAAA",
"subTitle": "aaaa",
"title": "String"
},
"paymentDate": "2014-02-19T15:20",
"levelOfCare": "Full-Time",
"units": "10"
}
As seen above the response expects agencyPercentagePayment to be first element of Outer levelOfCare as first element after completion of benCoDtls array and paymentDate after levelOfCare, however the response is different as you see.
Any suggestions how to enforce the response generated by the Jersey Jackson Providers? I can share the Java class if required.
Thanks
You should be able to fix this using #XmlType annotation:
#XmlType(propOrder={"agencyPercentagePayment", "agencyProviderPaymentAmount" , ... })
#XmlRootElement
public class Outer {
....
}