node-RED json object - how to extract a value example code - json

I am trying to extract values from a json file. I can do this with function node when it is posted from a webhook on my github page. But from another site I cant seem to get this working. This is what I use to get the value on github to test.
var msg2 = {payload: msg.payload.comment.user.login };
return msg2;
This is the code I get from the other server. I just want two value from this below. When I save the incomming message directly with a save node this is whats in the json file.
{"payload":"{\"id\":33,\"status\":\"completed\",\"totalAmount\":103,\"tipAmount\":0,\"feeAmount\":4,\"settleAmount\":99,\"requiredAmount\":null,\"date\":\"2017-09-22T06:51:33Z\",\"snapCode\":\"To9tQhiV\",\"snapCodeReference\":\"06ce2405-1688-4cb7-aa26-6df25c053f8c\",\"userReference\":\"\",\"merchantReference\":null,\"statementReference\":null,\"authCode\":null,\"deliveryAddress\":null,\"extra\":{\"merchantReference\":\"AB11FFBC56\"}}"}
I just need totalAmount and merchantReference in the extra field.
Please assist and thank you in advance.

Your payload is a JSON String. To access its contents you must first parse it to a JavaScript Object.
Add a JSON node in front of your Function node - that will convert msg.payload for you.

Related

Unable to Extract value from JSON response which has the data in "\"

Please help me to get out the problem which I am facing. I have a JSON response like this:
"{"Version":10,"Collections":[{"Id":"*******","Name":"","Description":null,"IsValid":false,"ABC":"\\XYZ\\collection\\","C
and want to fetch the values of collections which has n number of Id's but JSON extractor is not working here.
Don't post text as image otherwise even those who are willing to help won't re-type your code or use OCR software in order to reproduce your issue
What you're showing to us is not a valid JSON, you can check it yourself using an offline or online JSON Lint Tool hence you won't be able to use JSON Extractor for this.
If you get escaped JSON as a part of some JSON attribute, like this one:
{
"some-attribute": "{\"foo\":\"bar\"}"
}
You won't be get this bar value directly so you will need to do this in 2 steps:
Use 1st JSON Extractor to get the value of some-attribute
Use 2nd JSON Extractor to get the value of foo attribute
Demo:
More information: API Testing With JMeter and the JSON Extractor

NodeJS Joi Vlidation - How to return JSON response instead of a string?

Recently, Iv'e been using the Joi validation library in-order to validate the data which comes in from a request (building a RESTful API).
I was using the .label() method to generate a string response and send it back to the user, but I couldn't find any way to send a JSON response back to the user?
Tried sending a premade JSON inside the string, searching the documentation and the internet of course - couldn't find any mention of it.
Current code example:
textField: Joi.string().required().max(4).label("This example field didn't pass the testing phase, please try again)"),
Any ideas?
If you need to send data in case of error,try .error() method. you can pass error in it.

Pentaho HTTP Post using JSON

I'm brand new to Pentaho and I'm trying to do the following workflow:
read a bunch of lines out of a DB
do some transformations
POST them to a REST web service in JSON
I've got the first two figured out using an input step and the Json Output step.
However I have two problems doing the final step:
1) I can't get the JSON formatted how I want. It insists on doing {""=[{...}]} when I just want {...}. This isn't a big deal - I can work around this since I have control over the web service and I could relax the input requirements a bit. (Note: this page http://wiki.pentaho.com/display/EAI/JSON+output gives an example for the output I want by setting no. rows in a block=1 and an empty JSON block name, but it doesn't work as advertised.)
2) This is the critical one. I can't get the data to POST as JSON. It posts as key=value, where the key is the name I specify in the HTTP Post field name (on the 'Fields' tab) and the value is the encoded JSON. I just want to post the JSON as the request body. I've tried googling on this but can't find anyone else doing it, leading me to believe that I'm just approaching this wrong. Any pointers in the right direction?
Edit: I'm comfortable scripting (in Javascript or another language) but when I tried to use XmlHttpRequest in a custom javascript snippet I got an error that XmlHttpRequest is not defined.
Thanks!
This was trivial...just needed to use the REST Client (http://wiki.pentaho.com/display/EAI/Rest+Client) instead of the HTTP Post task. Somehow all my googling didn't discover that, so I'll leave this answer here in case someone else has the same problem as me.
You need to parse the JSON using a Modified JavaScript step. e.g. if the Output Value from the JSON Output is called result and its contents are {"data"=[{...}]}, you should call var plainJSON = JSON.stringify(JSON.parse(result).data[0]) to get the JSON.
In the HTTP Post step, the Request entity field should be plainJSON. Also, don't forget to add a header for Content-Type as application/json (you might have to add that as a constant)

Angular and JSON, having trouble parsing for ng-repeat

I'm horribly sorry if there is a post for this, I tried to search but didn't find a answer.
Problem:
I'm calling a web service and receiving not so well formed JSON data from a Dynamics Nav service:
JSON:
"[{\"type\":\"2\",\"number\":\"VHT3866\",\"location\":\"Delta\",\"destinationNo\":\"\",\"contactName\":\"Jesus\",\"shipToName\":\"Lord jesus\",\"highPriority\":\"false\",\"hasComment\":\"true\",\"assignedTo\":\"\",\"source\":\"\"},{\"type\":\"2\",\"number\":\"VHT3866\",\"location\":\"Delta\",\"destinationNo\":\"\",\"contactName\":\"Jesus\",\"shipToName\":\"Lord jesus\",\"highPriority\":\"false\",\"hasComment\":\"true\",\"assignedTo\":\"\",\"source\":\"\"}]"
I then take this JSON and use angular.fromJson(json) to get it properly.
It doesn't seem to change into an array of javascript objects, but just plain text.
However if I take the same JSON and just put it manually in like this:
var json = angular.fromJson(stringfromserver);
It turns into a proper javascript object and ng-repeat throws no error.
I found an answer on Quora:
--- Le Batoure,
Angular from json is now strict so assuming that this string is from a trusted source you would have to use "eval()" plus surround the call in parenthesis for it to work
var hatsData = angular.fromJson(eval("(" + hats + ")"))
If you bring your JSON from http request for example you don't need to use the fromJson method.
The JSON is automatically parsed by Angular and you can use it directly.

SAPUI5 oModel.getJSON() isn't working when I use a JSON file as origin

I created my Model using a JSON file.
var oModel = new sap.ui.model.json.JSONModel( jsonFileUrl ); //JSON from file
It worked and the element was populated as I wanted.
But after this, when I tried to use oModel.getJSON() to get the JSON data it didn't work.
If I use an variable with the same content as the file, it works!
You can check the full test that I created:
https://googledrive.com/host/0B2gJUv6a_V1dYnNSV0ZsTFhxazg/index.html
Is there anybody to help me to understand what on Earth is happening here?
It's because at the time you try to emit the JSON here:
$("#jsonFile").append(oModelFile.getJSON());
the actual ajax request to retrieve the file hasn't completed, and so the JSON model isn't filled at that time.
Wrap this in a handler for the requestCompleted event like this and it will work:
oModelFile.attachRequestCompleted(function() {
$("#jsonFile").append(oModelFile.getJSON());
});