I have a simple problem where I am not able to insert an array in json payload to call graph api to update user profile while using Bot Framework Composer.
Example:
Step1. I created a property Conversation.UpdatedSkills which holds following value
["SharePoint","Dotnet","Analytics"]
Now I want to build JSON object for MS Graph service and working sample payload looks like this.
{
"skills":["SharePoint","Dotnet","Analytics"]
}
Step2. Now to build this JSON dynamically, I need pass body as JSON Object in Send an HTTP Request activity and I have folllowing code to generate payload.
{
"skills":"${conversation.UpdatedSkills}"
}
The output from Step2 looks something like this.
{
“skills”: “[\r\n “SharePoint”,\r\n “Dotnet”,\r\n “Analytics”\r\n]”
}
DESIRED JSON WAS THIS:
{
"skills":["SharePoint","Dotnet","Analytics"]
}
My question is, How do I pass my array from step 1 such a way so that it creates json object that works in service. The object created using step 2 is not the right object that service takes.
Any idea?
I tried different string manipulations but I think this is basic and there has to be something.
Don't use the string interpolation ${ }, but use an expression (=). For your example above, it should be:
{
"skills": "=conversation.UpdatedSkills"
}
Related
I created a Json Server Database like this:
"Time":
[
{
"id":1,
"name":
[
{
"id":1,
"checkin":
[
{
"id":1,
"date":"123",
"time":"123"
},
{
"id":2,
"date":"123",
"time":"123"
}
]
},
{
"id":2,
"checkout":
[
{
"id":1,
"date":"123",
"time":"123"
}
]
}
]
}
]
I don't want to get the entire Database and go through it. I just want to tell the Database where exactly my Object is and have it returned.
How would I call the call for example the first Check-in Object?
I use the Angular HttpClient like this:
this.http.get(endpoint, JSON.stringify(time), this.httpOptions))
So I need the Exact Endpoint in a format like: endpoint/id/id or similar
I imagined it like this: endpoint/time/1/1
With output:
[
{
"id":1,
"date":"123",
"time":"123"
}
]
If this is not possible please tell me anyways.
PS: The question from this thread is essentially the same as mine. Also the JSON documentation doesn't real help either, it just says you need custom routes for multilayer JSON strings but not how to implement these routes.
I'm not sure if I understand correctly where you are returning the data from. If you meant json-server, just look at the documentation (here) and then you could use an endpoint like "/posts?Id=2"
However, if you mean your own API, which does not have an endpoint that returns one record, e.g. by its ID, the only convenient solution is to create a service that will map the result from the server and return the desired value.
You can do all this in one place, but for clearer code, I recommend dividing it into:
service that will download data from the server
service that will map the data on the basis of a given parameter
component that will display the data
Below is a working example on Stackblitz.
example
Note that in the app-component I pass the ID 32 to the method from the mapping service as the parameter. The mapping service then calls a method that sends the request for all the data.
The important thing is that all data is returned to the application, not just one record. If the API you are using does not provide such an endpoint, it is not possible to return only one record.
Apparently a request like I wanted to call is still not possible. The only way to come close is to fake it with custom Routes and flattening the JSON structure like in this old thread.
I have an application that works for test purposes with local JSON mock data. The oData Object contains arrays with values and the application works as desired.
Now we are switching from the local mock data File to data consumption with an oData service from a SAP backend system.
Here I get the data in JSON Objects and not all the functionality works as desired (example filter functions).
Can somebody share some thoughts about JSON Objects and Arrays with me?
And how can I get the data from the backend system in an array instead of an object?
In the mock data version I do this to define my model:
this._oModel = new JSONModel(jQuery.sap.getModulePath("myApplication", "/localService/mockdata/nodesSet.json"));
In the oData Version the model is defined in manifest.json:
this._oModel = this.getOwnerComponent().getModel();
Note: I am aware of the different names of the entities (example: nodes vs nodesSet) and this is not part of the problem.
Thanks!
One simple way would be not to create the model in the Manifest and do an explicit read in the controller as:
var oModel = new sap.ui.model.odata.v2.ODataModel(url, true);
that=this;
oModel.read( "/Products", {
urlParameters: {
"$skip": 0,
"$top": 50
},
success: function( oData) {
//here oData shall have an array of objects "results"
**-------------Set the model here using this array -> results ------**
},
error: function(oError) {
alert("error");
}
});
I am however not proud of this solution and will check if I can comment better.
In my coffeescript frontend I attempt to pass a list of value to the backend
data = {
id: [2, 3, 4]
}
$.post url, data
In the handler in a Google app engine (python) backend, I read the value like so:
id_value = self.request.get('id')
LOG.info("%s", id_value)
It always print out '2' only.
How can I get the backend to obtain the list [2,3,4]?
$.post by default sends data in url-encoded format, which handles nested structures in its own way.
You might need to encode the data in JSON before sending and then decode it on the server side - an example is here.
The request object provides a get() method that returns values for arguments parsed from the query and from POST data.
If the argument appears more than once in a request, by default get()
returns the first occurrence. To get all occurrences of an argument
that might appear more than once as a list (possibly empty), give
get() the argument allow_multiple=True.
Hence you should use something like the below snippet. You can find more details here.
id_value = self.request.get('id', allow_multiple=True)
If you need to access variables url encoded in the body of a request (generally a POST form submitted using the application/x-www-form-urlencoded media type), you should use something like this.
id_value = self.request.POST.getall('id')
I want to build my endpoint, which accept JSON array of below format:
[
{
"test":"Math",
"result":"Pass"
},
{
"test":"Science",
"result":"FirstClass"
}
]
It will be a POST call with the above JSON to my endpoint.
I tried it with servlet too but did not get the required result, and also tried to with list and inserting in a new class and posting to that class. Thanks in advance.
Is that an accurate representation of the JSON object which is being sent over? Because one does not simply send a a POST request with a JSON object of their param object to a cloud endpoint. See here for a thorough guide to Endpoint API interaction from a javascript perspective - notice how the client library exposes an object "gapi" through which calls are made. If you're sending this JSON from an iOS or Android app, there are similar client libraries which can be generated for you by a cloud endpoints build tool.
After much frustration, I resorted to reading the docs more carefully. In that quest, I found an important note in the doc:
https://cloud.google.com/endpoints/docs/frameworks/java/parameter-and-return-types
"Any type except a parameter or injected type is considered an entity type. ... Entity types cannot be annotated with #Named"
With all examples showing named parameters, I was stumped as the docs don't explain further, but then found a solution. It ends up that if you do not have named parameters, everything is just passed in as a LinkedHashMap. Usually, you can do any work you need to with just that data structure, but if you HAVE to have it in JSON, you can convert it. Here are some examples:
#ApiMethod(name = "endpointIterfaceName.createItems", httpMethod = "post", path = "test/items")
public WhateverReturnType createItems(LinkedHashMap<String, Object> itemsMap) {
// Do Stuff with map values
return whateverReturnValue;
}
With this, you need to be sure that you post your data with the Content-Type of json (i.e. Content-Type:application/json; charset=UTF-8). So, for example, when testing, with a jquery ajax call you would need to set dataType to "json" or with Postman, you would select "Raw" then JSON (application/json).
If you really want to convert this to a JSON object in Java because for whatever reason you can not use a hash map, you can do the following in your method:
// Use gson library to convert the map to a string
Gson gson = new Gson();
String mapAsJsonString = gson.toJson(itemsMap);
// create a JSON object from the new string representation
JSONObject obj = new JSONObject(mapAsJsonString);
As a side note, if this is passed as Content-Type:text then the whole body will be in the map as the first key of the map. You could do some inadvisable things here and just get that key and avoid converting the map to a string and then to a json object, but, like I said, that is inadvisable. :)
Restangular.one('suppliers', 'me').getList('websites').then(
(data) ->
$scope.websites = data
$scope.websites.patch()
)
I'm just trying this for a quick test.
So the api call on /suppliers/me/websites returns an array but when I try to patch from the Restangular object it sends the data splitted as you can see below.
[{"0":"h","1":"t","2":"t","3":"p","4":":","5":"/","6":"/","7":"w","8":"w","9":"w","10":".","11":"p","12":"f","13":"c","14":"o","15":"n","16":"c","17":"e","18":"p","19":"t","20":".","21":"c","22":"o","23":"m"}]
I'm new to Angular & Restangular , what am I missing ?
Edit : To be clear, I insta patch for the test but normally I modify the websites array by adding / removing.
It looks as if you're returning a string from your service, whereas a valid JSON response is expected by Restangular.
For example:
[{"website": "http://www.example.com"}, {"website": "http://www.domain.com"}]
EDIT: I just noticed that in your question, you say your service returns an array. Double-check what it does return and make sure that it is valid JSON.
EDIT 2: It seems that Restangular expects not only valid JSON, but also JSON formatted as my code sample above is (ie. [{"key": "value"}] and not ["value"].