Parsing JSON with Node Red with telegram bot - json

I am trying to get JSON working with my telegram bot which I have already created. I can the bot send and receive message in the debug screen from telegram in Node Red.
I want to take the return api message from telegram and then parse it out to eventually have it do something like turn on the led if I send it "LED-ON" command or similar.
Currently I see this type of JSON format. And I want to basically parse the content field out from the JSON object to get me LED-ON.
{
"chatId":64XXXXX7,
"messageId":337,
"type":"message",
"content":"LED-ON",
"date":"2017-09-09T07:07:38.000Z",
"inbound":true
}
I used the JSON node but from the debug it only changes the message from json object to json string. But I still can't parse out LED-ON.
Also if once i get LED-ON filtered and send it out to a split node to generate a MQTT message to turn the LED, do I need it to be a object or string? Sorry I just am very new to programming.
I can share flow if it dosen't make sense.

There is no need for the JSON node if the content is already a JSON object.
I'm at a loss as to why you would need a split node, a switch node or a function node should be all that is needed to test the value in msg.payload.content
The MQTT node will always convert any outbound msg.payload to a string before publishing.
EDIT:
All nodes (including function nodes) need to return an object. The msg.payload should normally hold the "output" from a node, Also no need to declare msg as it is already in scope, so in the case of your example it should be:
msg.payload = msg.payload.content;
return msg;
Also you may do better asking questions like this on the Node-RED Slack team (linked from the Node-RED home page) as it's likely to need a little back and forth, which Stack Overflow is not best suited to.

Related

Azure FCM NotificationHubClient Configuration for Data Payload?

Using
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName, enableTestSend);
NotificationOutcome outcome = await hub.SendDirectNotificationAsync(fcmNotification, deviceUri);
I am able to send and receive notifications using FCM via the Azure hub to a Xamarin Android app, finally. However the payload is not present in the received RemoteMessage even though the sent fcmNotification json payload looks good and passes validation. I am basically looking at the RemoteMessage.Data property, but not finding the expected payload array. Looking at RemoteMessage structure, I haven't found any part of the payload array either.
I know that the Azure hub manipulates the notification by adding the necessary headers like content type, e.g. "application/json". Are there any other settings that are needed to be passed to enable the "data" only payload?
Additional settings are not necessary, but the format of the entire notification contents has to have this type of structure:
"{ \"data\":{ \"A\": \"aaa\",\"B\": \"bbb\",\"C\": \"ccc\",\"D\": \"ddd\",\"E\": \"eee\",\"F\": \"fff\"}}"
The number of data elements is up to you. The data element name can be anything as well as the associated content except for the need for backslash usage for special characters. Both the element name and content can be inserted using variables creating a traditional composite string.
What is especially important is the inserted spaces as shown. Also note that traditional Json formatting is not acceptable because of the need for those spaces.

Angular 6 HttpClient not converting response to Interface

I have an api endpoint that responds with a JSON Array as a string.
Correspondingly, I have an interface that matches the JSON response
I have a service that makes the request to get the array of users and logs the first record to the console.
Expected Results
I expect to get a UserDetails object back and should print all the contents of index 0 to the console.
Actual Results
In the console I just see the character '['. It seems that res variable is still being treated as a string, and not a UserDetails array.
I have been struggling for house to try and figure out what is causing this behavior
I found the problem for anyone who is interested.
My server is returning the response in the body as a JSON string (this is a requirement from AWS API Gateway). The following has to be done to get the data to correctly parse.

Send custom property with value as JSON array

We want to send some events to Application Insights with data showing which features a user owns, and are available for the session. These are variable, and the list of items will probably grow/change as we continue deploying updates. Currently we do this by building a list of properties dynamically at start-up, with values of Available/True.
Since AI fromats each event data as JSON, we thought it would be interesting to send through custom data as JSON so it can be processed in a similar fashion. Having tried to send data as JSON though, we bumped into an issue where AI seems to send through escape characters in the strings:
Eg. if we send a property through with JSON like:
{"Property":[{"Value1"},..]}
It gets saved in AI as:
{\"Property\":[{\"Value1\"},..]} ).
Has anyone successfully sent custom JSON to AI, or is the platform specifically trying to safeguard against such usage? In our case, where we parse the data out in Power BI, it would simplify and speed up a lot some queries by being able to send a JSON array.
AI treats custom properties as strings, you'd have to stringify any json you want to send (and keep it under the length limit for custom property sizes), and then re-parse it on the other side.

What are developers their expectations when receiving a JSON response from a server

I have java library that runs webservices and these return a response in XML. The webservices all revolve around giving a list of details about items. Recently, changes were made to allow the services to return JSON by simply converting the XML to JSON. When looking at the responses, I saw they're not as easy to parse as I thought. For example, a webservice that returns details about items.
If there are no items, the returned JSON is as follows:
{"ItemResponse":""}
If there is 1 item, the response is as follows (now itemResponse has a object as value instead of a string):
{"ItemResponse":{"Items":{"Name":"Item1","Cost":"$5"}}}
If there two or more items, the response is (now items has an array as value instead of an object):
{"ItemResponse":{"Items":[{"Name":"Item1","Cost":"$5"},{"Name":"Item2","Cost":"$3"}]}}
To parse these you need several if/else which I think are clunky.
Would it be an improvement if the responses were:
0 items: []
1 item: [{"Name":"Item1","Cost":"$5"}]
2 items: [{"Name":"Item1","Cost":"$5"},{"Name":"Item2","Cost":"$3"}]
This way there is always an array, and it contains the itemdata. An extra wrapper object is possible:
0 items: {"Items":[]}
1 item: {"Items":[{"Name":"Item1","Cost":"$5"}]}
2 items: {"Items":[{"Name":"Item1","Cost":"$5"},{"Name":"Item2","Cost":"$3"}]}
I'm not experienced in JSON so my question is, if you were a developer having to use these webservices, how would you expect the JSON resonse to be formatted? Is it better to always return a consistent array, even if there are no items or is this usually not important? Or is an array not enough and do you really expect a wrapper object around the array?
What are conventions/standards regarding this?
Don't switch result types, always return an array if there are more items possible. Do not mix, for 1 item an object for more an array. That's not a good idea.
Another best practise is that you should version your API. Use something like yoursite.com/api/v1/endpoint. If you don't do this and you change the response of your API. All your client apps will break. So keep this in mind together with documentation. (I've seen this happen a lot in the past..)
As a developer I personally like your second approach, but again it's a preference. There is no standard for this.
There are several reasons to use json:
much more dense and compact: thus data sent is less
in javascript you can directly access those properties without parsing anything. this means you could convert it into an object read the attributes (often used for AJAX)
also in java you usually don't need to parse the json by yourself - there are several nice libs like www.json.org/java/index.html
if you need to know how json is build ... use google ... there tons of infos.
To your actual questions:
for webservices you often could choose between xml and json as a "consumer" try:
https://maps.googleapis.com/maps/api/place/textsearch/json
and
https://maps.googleapis.com/maps/api/place/textsearch/xml
there is no need to format json visually - is it not meant for reading like xml
if your response doesn't have a result, json-service often still is giving a response text - look again at the upper google map links - those are including a response status which makes sense as it is a service.
Nevertheless it's the question if it is worth converting from xml to json if there isn't a specific requirement. As Dieter mentioned: it depends on who is already using this service and how they are consumed ... which means the surrounding environment is very important.

String[] Input for REST Calls

I am new to REST Programming. I have a requirement to automate the working of an appliance which requires me to login to the appliance, use the auth token and perform other rest calls on the appliance subsequently. I have been able to login and perform routine GET calls and parse the output of the REST calls.
Now, the specification of the REST call that I am unable to understand is as following:
Call: PUT /rest/os-deployment-build-plans/<osbp-id>/run
Input: Std headers, String[] serverIds
Basically, this call is used to run a build plan against a server, I am unable to figure out how to give the String array as input. All the calls in this appliance take only JSON as input and accept only JSON types. Any pointers as to what this might mean? Is it a JSON string? Is it an array of JSON objects?