NodeJS create JSON array for Slack incoming webhook - json

I am currently working on getting my incoming Webhook application written in Node.JS to get work with this API: https://api.slack.com/incoming-webhooks.
My problem is that the JSON string for Slack has to configured with " these quotation marks and if I create a simple JSON string in Node.JS, Slack says that the string isn't valid.
var text = "New Commit";
var form = '{"text": "' + text + '" }';
The above code works, but looks pretty ugly and when I add a \n' to indicate a new line, the \n is directly converted into a new line and not sent within the string, so Slack says again the JSON string isnt valid.

As mentioned by tandrewnichols I need to use JSON.stringify and create an object.

Related

extract a concrete part of a value from a Response to a new string Groovy script

I'm kinda new in groovy and I need help
I need to write a groovy script to transform or else extract a concrete value from a Json Response to a new string Groovy script.
and the groovy script I used is this one :
responseJson = testRunner.testCase.getTestStepByName("Test Scenario").getPropertyValue("response")
parsedResponse = slurper.parseText(responseJson)
log.info(parsedResponse["items"]["/ticket_id"])
My generated string response from the groovy script I use looks like this:
/ticket_id":"{\"isTodo\":false,\"items\":[[\"WhatIwantToExtract\",\"\",\"url.com:blablabla_qc_vpc-11:Machine:data-da-data\",timestamp]]}
The response is correct in fact but I just want to extract a piece of this
The data I wanted to extract is labeled above as " WhatIwantToExtract " without the commas.
I solved it
def regexResult = (parsedResponse["items"][0][0])

how to remove backslash added json.stringify

i'am currently working on a object to string conversion and upon reading i need to use json.stringify. I have successfully converted it, however when i try to input a backslash on my javascript object the conversion adds another backslash.
Scenario:
on my object variable i used "\r\n" after using the json.stringify the result was "\r\n" which should be only "\n" as i used the .replace function
var str = JSON.stringify(item.NotesContent);
var exp1 = str.replace(/\r\n/g, "\\\\n");
console.log("your new string is : " + exp1);
Result
any inputs for this?

nodejs json_parse crash on asteriks

I listen on a websocket for some data that will be in the following format:
'{"mode": "test", "code": "' + editor.getValue() + '", "testTeam": "basic"}'
The users will write some code that we then will run in a sandbox.
When i parse the data with data = JSON.parse(message); it crash if it get the character * asterisk.
What is so special with * that makes it crash? I though of just removing them but that will destroy user comments in the code.
Instead of this:
'{"mode": "test", "code": "' + editor.getValue() + '", "testTeam": "basic"}'
use this:
JSON.stringify({mode: "test", code: editor.getValue(), testTeam: "basic"})
to have a correct JSON string.
What probably happens is that editor.getValue() contains quotes or newlines and you are not escaping them correctly.
This is just a guess because you didn't provide an actual example of message before parsing but you should never compose JSON directly with string concatenation. Use JSON.stringify() to serialize JavaScript objects to JSON.
Also always put JSON.parse() and JSON.stringify() inside of try/catch to avoid crashing on bad input or use a module like tryjson that does that for you:
https://www.npmjs.com/package/tryjson
Both JSON.parse() and JSON.stringify() can throw exceptions.

Azure Stream Analytics Error :Could not deserialize the input event as Json

I am trying to create a Stream Analytics job. The message is being sent in the following format as JSON:
var message = "Name;\n" + Guid.NewGuid().ToString() + ";" ;
When I am running my job I am getting the following error:
Could not deserialize the input event as Json. Some possible reasons:
1) Malformed events
2) Input source configured with incorrect serialization format
Based on your code sample, it appears your input is taking the form of:
Name;
AA7509E7-D482-459B-9689-456A0F952B44;
then the error message you're seeing is correct, this is not valid JSON, so ASA won't be able to deserialize it. Your JSON string should look something like this:
{
"Name": "AA7509E7-D482-459B-9689-456A0F952B44"
}

WebClient.DownLoadString is adding \" infront of my JSON data elements.How to parse it as normal JSON without \"?

I am trying to access a REST Service in my MVC application.I am calling getJSON method to get the data from a controller which internally calls the REST service which returns data in json format.But I am getting the a lot of "\ in my output of DownLoadString method and my return Json is not returning proper JSON data and hence my client side script is not able to access the JSON properties.
My Script in my view is
$.getJSON("#Url.Action("GetManufacturers", "Home")",function(data){
console.debug("Status is : "+data.Status)
});
My Action method looks like this
public ActionResult GetManufacturers()
{
string restURL ="http://mytestserver/myapi/Manufacturers";
using (var client = new WebClient())
{
var data = client.DownloadString(restURL);
//data variable gets "\" everywhere
return Json(data,JsonRequestBehavior.AllowGet);
}
}
I used visual studio breakpoints in my action method and i am seeing a lot of \"
And i checked what is coming out to my getJSON callback and the JSON tab is empty.
But my response tab has content like this
I belive if there is no \", i would be able to parse it nicely.
I used fiddler to see whether i am getting correct (JSON format) data from my REST service and it seems fine.
Can anyone help me to tackle this ? I would like to return proper JSON from my action method. Sometime i may want to read the json properties in the C# code itself. I saw some example of doing it with DataContractJsonSerializer. But that needs a concrete type to be converted to. I don't want to do that. because other clients would also access my RESTService and how will expect them to write a fake entity for this ?
You need to return the data as is:
public ActionResult GetManufacturers()
{
string restURL ="http://mytestserver/myapi/Manufacturers";
using (var client = new WebClient())
{
var data = client.DownloadString(restURL);
return Content(data, "application/json");
}
}