How to transform JSON in Azure Logic Apps? - json

I'm trying to create an Azure Logic App that broadly does the following:
Use a HTTP call to a REST service, the REST service will return JSON with Ids. This is working fine, the resulting JSON looks a bit like this: "workItems" : [ { "id": 118, }, { "id": 119, }, etc ]
I need to extract all the Ids, and put them into a comma separated string, e.g. 118, 119, etc.
The comma separated string will then be used as part of another HTTP REST call.
However I'm struggling at point 2. I cant see where I can write some script or code (without building a custom logic app component) to do this transformation.
At the moment I've tried using the BizTalk Apps to convert the JSON to XML, then use XPath, then hopefully get that back into a string at some point - but this whole process seems overly complicated.
I realise I could write a custom app, but if I did that then I might as well just do all the work in the custom app as well. Be nice to use the native features of Azure if possible.
I'm afraid I might be missing something obvious. Suggestions would be appreciated.

Try the CsScripting Api. It enables you to do some simple c# code and has the NewtonSoft libs available. I usually write the code as a console app for testing first before plugging it into the logic app action.

WebJobs Webhooks are now deprecated. Use Azure Functions Generic Webhooks instead - they have direct integration support with Logic Apps.
One option is to use a WebJob Webhook and do the transformation/filter there. I have an example on GitHub of using this to filter posts to Slack. If you already have a Web/Mobile/API app up and running, it's easy to have a WebJob hosted on it so you don't need additional resources, necessarily.
Your other option, you highlighted. Deploy an API App which will do the xform for you.
If you want to go down the WebJob route and need any help, let me know and I'll be glad to assist.

Related

Parsing last message from Pushbullet using Selenium

I'm trying to parse the last received message from Pushbullet. I'm currently doing it using Clicks, which means that I do every single clicks, sendkeys and all the rest needed actions automatically. In other words it is just a simulation of the user. One of the biggest con of the method is that whenever xpath or a class or id of the element changes which I'm aiming with Selenium, whole cycle fails and the test case can not proceed operating.
I want to change the way I'm doing. Particularly, I want to send a json request to Pushbullet API and then get the response in return.
I just couldn't decide from where and how to start doing.
Could you guys please tell me the way from where should I start and what are the steps that needs to be covered in order to finally reach the finish line?
Thank you in advance.
I noticed that this question is tagged under Selenium, but in your question, you express interest in switching to an API approach. I will try to provide some advice to you on this.
Selenium Approach
You mention that you tests are brittle, and if anything changes then they fail. This is usually the case with UI tests. If you would like to stick with the Selenium approach, I can try to help you write more robust locators for your WebElements that will not break constantly.
API Approach
You will need to start with the Pushbullet API documentation -- https://docs.pushbullet.com/
To get messages, it looks like you will want to use the chat endpoint -- a sample request looks like this: https://api.pushbullet.com/v2/chats, plus authentication.
Once you fetch the chat objects, you will need to write your own logic to parse each chat object and fetch the most recent message from there.
Depending on what language you are using, you will need to install a REST client package onto your project. I use C#, so RestSharp is the client I like to use.
I recommend installing a REST client interface, such as Postman, to start practicing your API calls. Once you get your API calls working in Postman, you can start writing code to make these API calls.
What other questions do you have about this?

Non-standard JSON and Azure Logic Apps

I have an API that produces JSON like this:
)]}',
{
//JSON DATA
}
The //JSON DATA is valid JSON, but the )]}', up top is not.
When I try to GET this data via a Logic App, I get:
BadRequest. Http request failed: the content was not a valid JSON.
So, a few related questions:
1) Can I tell the logic app to return the invalid JSON anyway?
2) How can debug the issue better? I happen to know that the response is invalid, but what if I didn't? Can I see the raw data somewhere?
3) This is all done via the Azure web portal. Are there better tools? Visual Studio?
I should also mention that if I call a route on the same API that returns XML instead of JSON, then the Logic App works fine. So it definitely doesn't like the JSON response in particular.
Thanks!
First of all, please do not post three questions as a single question.
Question 1). The best thing you can do is make the API return a valid JSON object. This is good for million reasons. Here're a few:
it's pretty much a standard (either valid JSON or XML -- yeah, old school way);
therefore, no users of this API (including you) will need to struggle and guess what's going on and why;
your Logic App's step will just work without adding extra complexity;
you will make this world and your karma better.
If API-side changes are not within your reach, I don't think you can do much. If you're lucky and the HTTP action is successful (Status Code 2xx), you can try to use a Query Action with a function that truncates the first characters. It will look something like this (I don't know the exact syntax): #Substring(body('myHttpGet'), 4, length(body('myHttpGet')) - 4) where myHttpGet is the id of the Http Get action.
However, once again, if possible, I strongly recommend fixing up the API which is the root cause of the problem, instead of dealing with garbage response after that.
UPDATE Another thing you can do is wrap the dirty API. For example, you could create a trivial Azure Function that invokes the API you don't directly control, and sanitizes the response for you consumption requirements. This Azure Function function should be easy to call from the Logic App. It costs almost nothing (unless we're talking millions of requests/month). The only drawback here is the increasing latency, which may be not an issue at all -- test it and see whether it adds less than 100ms or so... Oh, and don't forget to file a ticket with the API owner, they make our world a bad place!
Question 2) In Azure Logic App web UI you can Look into the execution details and the error will definitely be there.
Question 3) You're asking for a tool recommendation which is by definition a highly subjective thing and is off-topic on StackOverflow.
TL/DR: The other app is not producing valid JSON.
Meaning, this is not a problem for you to solve. The other app has to return valid JSON if the owner claims it should.
If they cannot or will not produce valid JSON, then the first thing you need to do is inform your management that you will have to spend a lot of extra time accommodating their non-standard format.

How to use JSON API without document

This might be a weird question but I am open for all the suggestions.
The background is I want to use script to automatically deploy/remove docker container on Jelastic, but unfortunately this part is not well documented in Jelastic official API document. Jelastic provided me a piece of sample code demonstrated how to use bash to create a new environment with a new docker container but it is not enough, I still don't know how to create/remove docker container by looking at the sample code.
Since Jelastic is using standard JSON API, I am wondering is there any tool which can automatically retrieve/detect that the parameters I can use with Jelastic JSON API?
If you were me, how would you get over this if there is no document as reference?
I am keen to use Jelastic, but this issue stopped me from onboarding, many thanks.
J.
All the parameters that can be used with Jelastic JSON API are specified at http://docs.jelastic.com/api/ page.
To use JSON API without document I suggests to you check the Postman API tool https://www.getpostman.com/. This application allows you to see all the sent/received data and allows you to passes JSON values without any document or any additional actions.
Simplest scenario for beginners: Go to API docs, section Users>Authorization, using Signin method you should to obtain the session value, that is necessary almost for all further actions. Then you need to obtain information about environment, section Environment>Environment, at first you should to executes GetEnvs method, then using the application identifier of the environment that was obtained from the previous command you should to executes GetEnvInfo method. As a results of the described scenario you will get all parameters and values that can be used with Jelastic JSON API for certain type of the environment.

WSo2 API Manager 1.8.0 - JSON parsing issue

I am new to wso2 API Manager, trying to set it up expose my plain HTTP POST back end calls as a REST API. I am sure this is not a new pattern that I am trying to achieve here. The requirement is to convert the JSON data coming in (has array structures) into the HTTP URL query string parameters.
After some research through the documentation and other posts on this forum, decided to go with the script mediator that would parse the JSON data and convert it to a string that can be appended to the endpoint URL. Some how I am not able to achieve this.
I have followed the following post that seems to be very straight forward. As the original poster suggested, I am also not able to use getPayloadJSON() method. Even have trouble using the work around suggested there due to JSON.parse() not working.
Link
Also, this approach of editing the service bus configuration from source view does not sound like the correct option. Is there another elegant solution to achieve this? Thanks for the help in advance.
I was able to get both methods working by using an external script instead of the inline java script. Thanks

How can call the Alfresco REST API using Json String?

Please provide me some references to call WebScripts in alfresco remotely using JSON..
Alfresco has some default Webscripts ,I need to invoke these Webscripts in different Application remotely...
There is no documentation that I know of at the present time that documents all web scripts that expect JSON to be posted along with a schema that defines the expected JSON. Honestly, we haven't done a good job identifying which out-of-the-box URLs are actually public. Some are there just for the Share application's use and could change without warning.
With that said, you can go to http://localhost:8080/alfresco/s/index and see a list of web scripts. And if you drill down into the web script (click on the web script's ID), you can see the source code for the JavaScript controller or, if the web script is implemented in Java, you can see the full class name that implements it. You can then inspect the source to see what it is expecting.
Another way to do it is to use Firebug or your browser's developer console to watch the network calls that go from your browser to the repository tier. Many of these calls include JSON being posted to repository tier web scripts.
Assuming you're referring to getting a webscript to respond with a json, there are a few steps.
1. Create a webscript, and possibly set json the default format (in the webscript definition file, i.e. mywebscript.get.desc.xml, add a tag
<format default="json">argument</format>
Create a JSON controller too, ie. mywebscript.get.json.js. This script can do two things:
a) get json parameter (if you sent a json in): if (json.has('myparam')) myVar = json.get('myparam');
b) provide some data to the model, ie. model.docs = companyhome.children
Your webscript also needs to format this json for json response, i.e. mywebscript.get.json.ftl would look something like this:
{ "docs": [
<#list docs as doc> {
"name": "${doc.name}",
"prop": "${doc.properties["mymodel:myprop"]}"
} <#if doc_has_next>,</#if>
</#list>
]
}