How To Get Specific Response Json? - postman

{
"id": 115084015,
"from": "help#email.ncloud.com",
"subject": "Verify Code",
"date": "2022-12-27 19:53:45",
"attachments": ,
"body": " Verification Code : 9906 Enter the code to sign up! Keep this code to yourself, and no other action is required. "
}
how do I want to retrieve the submitted code that appears from the json response?
I want to get just the response code to put in the variable value

I assume that you want to get 9906 from body part.
const res = pm.response.json();
let body = res.body;
const regex = /\d{4}/gm;
//console.log(regex.exec(body)[0]); //9906
pm.environment.set("code", regex.exec(body)[0]);

Related

how to store request parameter in a variable and used it another request

my request is:
{
"username": "test"
}
i want store this request in a variable and again called this in another request., I tried it for response part, but not sure how to achieve it for request.
You can do this.
Request 1:
I assume you have body like this
{
"username": "test"
}
Tests tab: Save a part of body in a variable
const body = JSON.parse(pm.request.body.raw);
pm.environment.set("username", body.username);
Request 2:
Body tab: Use this var as a normal variable.
{
"username": "{{username}}",
"pass": "11111"
}

Postman Variables

I am trying to parse "Id" from below json to store it in a variable
{
"cf35864e-944d-11e9-8aff-22000ab8d684": {
"Id": "1a45b704-944e-11e9-8aff-22000ab8d684",
"Name": "Test plan",
"Type": "Limited",
"Credits": 10119.70,
"AvailableCredits": 500.100
}
}
I've tried
bodydata =JSON.parse(responseBody)
planid=bodydata.cf35864e-944d-11e9-8aff-22000ab8d684.Id
console.log(planid)
But postman throws errors, is there any way so that only ID can be fetched.
This should work: planid=bodydata["cf35864e-944d-11e9-8aff-22000ab8d684"]['Id']
As per your mentioned JSON data, the below may work.
let resp = pm.response.json();
console.log(resp.cf35864e-944d-11e9-8aff-22000ab8d684.Id);
Above will work only if "cf35864e-944d-11e9-8aff-22000ab8d684" value is static and only one object is returned.

Postman Printing specific information from Response

I'm using Postman's console to display the response of the API Call with console.log, I'm using the runner since I have a lot of iterations. However, a lot of information from the API response are giving me trouble, so I would like to do is to print with console.log specific information of the responseBody.
As test with Postman, I'm using the following:
var body = JSON.parse(responseBody);
console.log(JSON.stringify(body.data));
The response is:
[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]},
What I would want to print with console.log would be only the values of device, time and data:
{"1BED7",1505320342,"05b006bcac00000000000000"},{"1CED7",1505277142,"05b006bcac00000000000000"},{"152C3",1505233937,"05b006bcac00000000000000"},
and so on...
My programming skills are very limited so sorry if the answer is so obvious, I have tested a lot of things but I'm still stuck.
Thanks a lot if you can help
I think your your response is array of objects.It is already a json object.So firstly what you are doing wrong is you don't need to parse it.You can directly use it.Check this below code snippet at the end of the answer.I think this satisfies your need.I used the forEach function to iterate through the the response array and push value that you need into the empty array.This result array contains object in following format.you can access each property of each object of this array by javascript . operator.I think that is quite obvious to you.
[
{
"device": "1BED7",
"time": 1505320342,
"data": "05b006bcac00000000000000"
},
{
"device": "1CED7",
"time": 1505277142,
"data": "05b006bcac00000000000000"
},
{
"device": "152C3",
"time": 1505233937,
"data": "05b006bcac00000000000000"
},
{
"device": "1BF81",
"time": 1505190735,
"data": "05b006bcac00000000000000"
}
]
var responseBody=[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},
{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]}];
var array=[];
responseBody.forEach(function (obj) {
array.push({device:obj.device,time:obj.time,data:obj.data})
})
console.log(array);
let results = _.map(JSON.parse(responseBody),
(sensor) => { return [sensor.device, sensor.time, sensor.data]});
// results contains an array like
// [[deviceId1, time1, data1], [deviceId1, time1, data1], ...]
console.log(results);

What is the correct way of parsing this json? Using ArduinoJson

I can't get the correct way to parse this JSON using ArduinoJson library
Here is the resulting json I need to parse.
{
"Error": false,
"Message": "Success",
"Sensor": [
{
"id": 10,
"mac_address": "aabbccddeeff",
"status": "ON"
}
]
}
May target value to get is the value of "status"
I tried to get some value and println it, but I got nothing?
Codes I tried so far.
StaticJsonBuffer<100> jsonBuffer;
HTTPClient http;
http.begin(path);
int httpCode = http.GET();
String payload = http.getString();
JsonObject& root = jsonBuffer.parseObject(payload);
String state = root["Sensor"];
Serial.println(payload);
Serial.println(state); //Print request response payload
Upon searching, I found that the author create an assistant to convert your defined json string.
You can find it here
https://bblanchon.github.io/ArduinoJson/assistant/index.html
Sensor is a list with a single object that has your desired property status.
String state = root["Sensor"][0]["status"];

Passing JSON data from response to request in Django

I have a Django (1.8.3) view that:
Makes a GET request to Server A (jetty), which returns JSON data in the body of the response. Then,
Makes a POST to Server B (node.js), passing the JSON data recieved from Server A in the body of the request.
The JSON data is structured like:
{
name: "foo",
details: {
"date": "today",
"isCool": "no",
},
stuff: [
{
"id": "1234",
"rating": "5",
}, {
"id": "5678",
"rating": "1",
},
]
}
But I can't figure out how to get the JSON from Server A's response into the request to Server B in my Django view. If I do this:
jetty_response = requests.request(method='GET', url=jetty_url)
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.json())
I get the JSON object in Server B, but it looks like this:
{
name: "foo",
details: [ "date", "isCool"],
stuff: [ "id", "rating", "id", "rating"]
i.e. the name property is correct, but the details dict is instead received as the keyset of the original dict, and the stuff list is received as a flat array of the keysets in all objects in the original dict.
If I instead do this in django:
node_response = requests.request(method="POST", url=node_url,
data=json.dumps(jetty_response.json()))
I get an empty object in node, and same goes if I do simply:
data=jetty_response.content
How do I make this request??
Figured it out myself.
As is usually the case, the simplest answer:
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.content)
worked fine once I took a closer look at my log and realized my POSTs were bouncing back 413, and then adjusted the size limit on my bodyParser in express.