How to compare JsonBody responses in Postman - javascript

I have one expected JSON Response Code and one, which I receive when I call an endpoint of an API.
What I try to achieve is:
that I can compare the JSON body together and see if they are matching.
At the same time, I want to be able, to "remove" one attribute of the JSON with his value like "Name".
So that everything can be compared expect the name key and his value.

Not sure if I completely understand your requirement but you can write your postman test as:
pm.test("response matches", function () {
var jsonData = pm.response.json();
delete jsonData.Name;
let str = JSON.stringify(jsonData);
pm.expect(str).to.include("stringified expected json data");
});
Refer: https://learning.getpostman.com/docs/postman/scripts/test_scripts/

Related

How to escape colon in JSON response field in postman test scripts

I'm writing a postman test script using the JSON response for a request, and one of the field name is named like aaa:bbb. How do I use this field in the test script? When I try using it as it is, I get a Syntax error.
My script is just trying to print the field value:
let res = pm.response.json();
res.results.forEach((e) => {
console.log(e.aaa:bbb);
})
I tried all of the following but they do not work:
e.'aaa:bbb'
e."aaa:bbb"
e.aaa\:bbb
e.jsonValue['aaa:bbb']
What is the correct escape character or the right way to do this?
Don't have too much experience with postman but have you tried e['aaa:bbb'].

Data coming from backend is stored in object but not accessible in object properties

Data coming from backend is stored in object but when i tries to print in using object properties its showing undefined. Here is my output:
enter image description here
my code:
this.documentService.getDocumentbyId(this.id).subscribe(
data => {
this.upmDoc=data;
console.log("UPM Data Fetched:",this.upmDoc);
console.log("Name:",this.upmDoc.name);
console.log("Symbolic Name:",this.upmDoc.symbolicName);
console.log("Object Store:",this.upmDoc.objectStore);
console.log("Active:",this.upmDoc.active);
},
error => console.log(error)
);
Its better to provide the response JSON instead of image to make sure where the problem is.
It looks like your service return HTTP Response not the body of the response. try to access the body of the data instead. replace the third line with:
this.upmDoc = data.body;
or
this.upmDoc = data["Document Details"];
based on your returned JSON.

POSTMAN - EXPRESS - Modify JSON before writing to file

I have written a POSTMAN call to a server that responds with a list of items in JSON like below:-
{
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
}
I want to make calls to another server using the value of the setID each time and iterate through all of these so that I end up calling the server thousands of times to verify the response from that server. The problem I have is that the second server expects the set id to be in a form where the forward slashes are converted to underscores and the hashes to dots, so
"1/7842/0889#001"
becomes
"1_7842_0889.001"
I have code that converts one to the other in POSTMAN
var jsonData = pm.response.json()
for (var i=0; i<jsonData.setIds.length; i++)
{
var new_j = jsonData.setIds[i].replace (/#/g, ".");
var new_i = new_j.replace (/\//g, "_");
}
})
This works fine line by line it creates the right thing in the console of POSTMAN but obviously what I really need to do is save the entire JSON in the right form to a file and then read from that file line by line using the corrected data. I don't seem to be able to save the data in a file in the right form using my code and I suspect I am missing something simple. Is there a way to write a file line by line from in side postman or in a script and manipulate the data as I'm creating it?
Alternatively I guess I could read from the JSON I have saved i.e. the full response and iterate through that manipulating the data as a pre-request script?
I have tried to do something like this using environmental variables - so in my first call I do:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable('setIds', JSON.stringify(jsonData));
and then in my second call to the express server where I want to send my payload I run a pre-request script that I thought would work using the env variable but this fails as it doesn't seem to like the {...
SyntaxError: Unexpected token {
I think there are probably some neat ways of solving this either doing all of this outside of POSTMAN in javascript but I'm a little lost where to start. Any help appreciated
Would tell you are plaing with content, but not setting it back to JSON object ??
jsonData.setIds[i] = new_i;
can help or you can use 2x replace it in a string and convert back to make it easier (in case there are no / or # somewhere else).
var src = {
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
//...
]
}
var str = JSON.stringify(src, null, 4);
str = str.replace(/\//g,'_').replace(/#/g,'.');
console.log(str);
var res = JSON.parse(str);
console.log(res);

Check contents of response to json request?

I am trying to build a random quote generator as per the FreeCodeCamp challenge, but I wanted to begin by just writing a test to confirm I'm actually getting the json object I'm requesting. I have a simple h1 element with the id set to 'quote' and the following code (jQuery is loaded up in the CodePen)
function genQuote () {
var output = $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(data){
var theQuote = data.content;
var Author = data.title;
document.getElementById('quote').innerHTML = theQuote;
});
}
The url in question, when visited, shows what looks plainly like a json object, to me, but my function does not change the #quote item at all.
Remove &callback= from URL, to request JSON, instead of converting $.getJSON() call to jsonp request. Also, an array is returned, not a plain object; access the object using bracket notation
function genQuote () {
var output = $.getJSON("https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1", function(data){
var theQuote = data[0].content;
var Author = data[0].title;
document.getElementById('quote').innerHTML = theQuote;
})
}
$(genQuote);
jsfiddle https://jsfiddle.net/gpyx6jLy/
As the comment to your question says, this is a Cross Origin Request Sharing (CORS) issue. The API is returning "http://null" as the Access-Control-Allow-Origin header, which disallows your access.
It looks like this is a bug in their API server, and you probably won't be able to use it.

Extracting data from JSON with JavaScript

Probably a stupid issue, but I am stuck.
I have a node.js server that sends data to a client, like this:
var message_to_client = result;
socket.send(JSON.stringify(message_to_client));
Client succesfully receives the data; if I log it on console, it reads like this:
[{"_id":"55e60d3de4b06ef3ed5f189e","par1":54.2441033, "par2":-10.177503399999999}]
I want to show the par1 value on screen, however I don't seem to be able to 'extract' it from the JSON response.
I tried with the parseJSON function from jQuery and with https://stackoverflow.com/a/22628619/3849735, neither worked.
Anyone can help? Thanks.
Send the response in json format instead in string, like
var message_to_client = result;
socket.send(message_to_client);
client side
data[0].par1
will return the values

Categories