JSON.stringify on javascript with WSO2 - javascript

i'm trying to transform the following JSON in input:
{
"operation": "create",
"id": "$1",
"name": "esempio create",
"type": "CAR",
"status": 0,
"country": "JAP",
}
in this new format:
{
"operations": [
{
"operation": "C",
"element": {
"type": "CAR",
"other_data": {"id":$1, "name": "example", "status":0, "country":"JAP"}
}
}
]
}
i'm using the following method, where element is the JSON mentioned above:
var js=JSON.stringify({"operation":"C", "element":{"type": element.type , "other_data":{element}}});
in every javascript compiler it works correctly, when i try to apply it on WSO2 i'm not able to save the page because the IDE (Integration Studio) detect an error on javascript.
Do you know any other way to do it or tell me why i'm not able to save it?
Thanks

Your syntax is correct but Integration Studio doesn't seem to like inline access of JSON elements when creating the JSON. Also you don't need JSON.stringify here. Following is a workaround with the Script Mediator. I assume you need t delete type elemt from other_data as well.
var element = mc.getPayloadJSON();
var js = {"operation":"C", "element":{"type": "x" , "other_data":"x"}};
js.element.type = element.type;
delete element.type;
js.element.other_data = element;
mc.setPayloadJSON(js);
Alternatively you can use the Payloadfactory Mediator along with Enrich mediator if you are using the latest Micro Integrator.
<payloadFactory media-type="json">
<format>{
"operations": [
{
"operation": "C",
"element": {
"type": $1,
"other_data": $2
}
}
]
}</format>
<args>
<arg evaluator="json" expression="$.type"/>
<arg evaluator="json" expression="$"/>
</args>
</payloadFactory>
<enrich>
<source clone="false" xpath="json-eval($.operations[0].element.type)"/>
<target action="remove" type="body"/>
</enrich>

Related

Can't access variables of JSON response from REST-API in Javascript

I have a problem getting access to the variables in a JSON-File. I get the file as a response from a REST-API. Here you can see the JSON-File:
{
"_embedded": {
"events": [
{
"name": "Josh.",
"type": "event",
"id": "Z698xZC2Z17CebP",
"test": false,
"url": "https://www.ticketmaster.de/event/josh-tickets/382625?language=en-us",
"locale": "en-us"
I want to access the value (Josh) of the "name" variable in Javascript in line number 5 and store it into a new variable to use it later.
This ist how I try to do it:
var = data._embedded.events[0].name
But I can't get access to the value attached to "name".
I have read the answers to similar questions here, but there was no solution for me.
I have only read, that it does not work because of the wrong JSON-structure. In other threads, they say there has to be a "{" instead of a "[" in line 3. But I don't know how to change it in javascript directly. I tried it with the JSON.stringify()-method but it didn't work out.
Can somebody help me, please? Is there any way to get access?
I hope you can understand my problem.
Thank you very much!
It seems that your json structure has some errors. The json structure similar to the following is legal. Of course, it is no problem to use a legal structure to exec data._embedded.events[0].name.
const data = {
"_embedded": {
"events": [
{
"name": "Josh.",
"type": "event",
"id": "Z698xZC2Z17CebP",
"test": false,
"url": "https://www.ticketmaster.de/event/josh-tickets/382625?language=en-us",
"locale": "en-us"
},
{
"name": "Josh.",
"type": "event",
"id": "Z698xZC2Z17CebP",
"test": false,
"url": "https://www.ticketmaster.de/event/josh-tickets/382625?language=en-us",
"locale": "en-us"
},
//...
]
}
}
console.log(data._embedded.events[0].name)

Unsplash API triggering download

I'm following triggering guidelines in Unsplash documentation. So the endpoint is:
GET /photos/:id/download
This is the photo's example response:
{
"id": "LBI7cgq3pbM",
"width": 5245,
"height": 3497,
"color": "#60544D",
"urls": { ... },
"user": { ... },
"links": {
"self": "https://api.unsplash.com/photos/LBI7cgq3pbM",
"html": "https://unsplash.com/photos/LBI7cgq3pbM",
"download": "https://unsplash.com/photos/LBI7cgq3pbM/download", // don't use this property
"download_location": "https://api.unsplash.com/photos/LBI7cgq3pbM/download?ixid=MnwxMTc4ODl8MHwxfHNlYXJjaHwxfHxwdXBweXxlbnwwfHx8fDE2MTc3NTA2MTM" // use this one ;)
}
}
"Be sure to include any query parameters included in the URL (like the ixid)."
So my question is what is value od ixid=MnwxMTc4ODl8MHwxfHNlYXJjaHwxfHxwdXBweXxlbnwwfHx8fDE2MTc3NTA2MTM and how to get it?
Try the following
const ixid = new URLSearchParams(
new URL("https://api.unsplash.com/photos/LBI7cgq3pbM/download?ixid=MnwxMTc4ODl8MHwxfHNlYXJjaHwxfHxwdXBweXxlbnwwfHx8fDE2MTc3NTA2MTM").search
).get("ixid");
console.log(ixid);

strongloop id not storing as string

I'm currently using strongloop to develop an application. Although I have already specified my id to type string, when I store a record, it came out as a string of objects.
This is my model.json file
"id": {
"type:": "string",
"id": true
},
and the result in the psql database is
{"0":"P","1":"K","2":"U","3":"W","4":"7","5":"C","6":"E","7":"B"}
Can someone point me in the right direction of why this is the case? I used a npm library to generate the string.
var shortid = require('shortid32');
var newID = shortid.generate();
Anyone knows what the problem is?
Add below configuration to id :
"id": {
"type:": "string",
"id": true,
"postgresql": {
"columnName": "id",
"dataType": "character varying",
"nullable": "NO"
}
},

Get a specific element from JSON

I know this is a very simple and common question; I've already read some Q/A but I can't figure out how to solve my problem.
I have this short json from an AJAX call that execute a SPARQL query:
{
"head": {
"vars": [ "name" , "email" ]
} ,
"results": {
"bindings": [
{
"name": { "type": "literal" , "value": "Name Surname" } ,
"email": { "type": "literal" , "value": "name.surname#email.com" }
}
]
}
}
I'm searching name and email of a single user of the application, so
the result should be always made up of a single element.
What I want to retrieve is the "name" of the user.
I tried something like:
response["name"].value
//or
response[0]["name"]
//or
response.name
but always wrong.
How can I get the name value? Thanks to everyone who will help.
Try this
response.results.bindings[0].name.value
response.results.bindings[0].email.value
Update
Example
You can check out the fiddle created here
http://jsfiddle.net/uqxp4j73/
The code for this is as under
var x='{ "head": { "vars": [ "name" , "email" ] } , "results": { "bindings": [ { "name": { "type": "literal" , "value": "aadil keshwani" } , "email": { "type": "literal" , "value": "name.surname#email.com" } } ] }}';
obj = JSON && JSON.parse(x) || $.parseJSON(x);
console.log(obj);
console.log(obj["results"]["bindings"][0]["name"]["value"]);
alert(obj["results"]["bindings"][0]["name"]["value"]);
Hope this helps :)
In JSON, you always have to provide the full path to the property you like to reach. Assuming you have stored the parsed JSON in variable response, the following paths will get you corresponding value.
response.results.bindings[0].name.value for name
response.results.bindings[0].email.value for email
Recommend you to go through http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/ to get basic concepts of JSON.

Delete object from json tree by reference in JavaScript

I need to remove an object from an JSON tree. I know a reference to that object. Is there a nice way to do it via JavaScript or jQuery besides traversing the whole tree?
Example:
party = {
"uuid": "4D326531-3C67-4CD2-95F4-D1708CE6C7A8",
"link": {
"rel": "self",
"href": "http://localhost:8080/cim/party/4D326531-3C67-4CD2-95F4-D1708CE6C7A8"
},
"type": "PERSON",
"name": "John Doe",
"properties": {
"CONTACT": [
{
"category": "CONTACT",
"type": "EMAIL",
"key": "email",
"value": "john.doe#doe.at",
"id": "27DDFF6E-5235-46BF-A349-67BEC92D6DAD"
},
{
"category": "CONTACT",
"type": "PHONE",
"key": "mobile",
"value": "+43 999 999990 3999",
"id": "6FDAA4C6-9340-4F11-9118-F0BC514B0D77"
}
],
"CLIENT_DATA": [
{
"category": "CLIENT_DATA",
"type": "TYPE",
"key": "client_type",
"value": "private",
"id": "65697515-43A0-4D80-AE90-F13F347A6E68"
}
]
},
"links": []
}
And i have a reference: contact = party.properties.contact[1]. And I want to do something like delete contact.
You may delete it this way. I just tested it.
var party = {
// ...
}
alert(party.properties.CONTACT[0]) // object Object
delete party.properties.CONTACT[0] // true
alert(party.properties.CONTACT[0]) // undefined
Fiddle
UPDATE
In the case above party is a direct property of window object
window.hasOwnProperty('party'); // true
and that's why you can't delete a property by reference. Anyhow, behavior of delete operator with host objects is unpredictable. Though, you may create a scope around the party object and then you'll be allowed to delete it.
var _scope = {};
var _scope.party = {
// ...
};
var r = _scope.party.properties.CONTACT[0];
window.hasOwnProperty('party'); // false
alert(r) // object Object
delete r // true
alert(r) // undefined
It only works one way: a variable holds a reference, but there is no way given a particular reference to infer what variables hold it (without iterating over them and comparing).

Categories

Resources