Unable to parse JSON response in Postman - json

Trying to parse this JSON response and get the id Value
{
"responseStatus": "SUCCESS",
"size": 1,
"start": 0,
"limit": 200,
"documents": [
{
"document": {
"id": 26,
Using this script to parse the response and post the value as an environment variable, for some reason is not retrieving the expected response.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("id", jsonData.id);
Although it created the id variable in the environment, the value is empty.

Since the id is under an object in the documents array of the response, you should use this
var jsonData = JSON.parse(responseBody);
var id = jsonData.documents[0].document.id;
postman.setEnvironmentVariable("id", id);

Related

I can't reach JSON value Flutter

I can't reach my returned JSON data value. I'm using get method for reaching and server returns the JSON with 200 but when I try to reach JSON value it gives error.
My JSON:
{
"user": [
{
"id": 2,
"userName": "Mehmet",
"email": "personal#test.com",
"sys_role": 3,
"tckn": "00000000000",
"fk_parkID": 81
}
],
"parks": [
{
"parkID": 1,
"parkName": "Park Name",
"latitude": 42,
"longitude": 29,
"fk_regionID": 2
}, // 107 more parks like the up one.
]
}
I've tried this for reaching "userName" value from "user".
var selectedUserInfo;
var parksInfo;
selectUser(id) async { // This is selecting a person for doing some process on his/her account.
var result = CallApi().getData('admin/user/${id}', _setHeaders());
selectedUserInfo = jsonDecode(result.body)["user"][0]["userName"];
parksInfo = jsonDecode(result.body)["parks"];
setState(() {
});
}
When I
print(selectedUserInfo)
it returns null.
Your getData() method is probably an asynchronous method (Future).
If you don't wait for it, the result will be null, so the selectedUserInfo.
You should add the await keyword like this :
var result = await CallApi().getData('admin/user/${id}', _setHeaders());
I hope that will solve your issue

Parsing JSON data from Postman

I am trying to parse a JSON response from Postman that is not cooperating. Here is the body response:
{
"teams": [
{
"id": "MI6",
"name": "James Bond's Workspace",
"color": "#04a9f4",
"avatar": null,
"members": [
{
"user": {
"id": 007,
"username": "James Bond",
"email": "blahblah#gmail.com",
"color": "#e65100",
"profilePicture": null,
"initials": "JB",
"role": 1,
"custom_role": null,
"last_active": "1609978205632",
"date_joined": "1609897014429",
"date_invited": "1609897014429"
}
}
]
}
]
}
I am using the following test scripts that can't seem to resolve the [ value after teams.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("teams_id", jsonData.teams.id);
postman.setEnvironmentVariable("teams_id", jsonData.teams.user.id);
It is creating the variable for me, but will not store the value of MI6 or the value of 007.
You're trying to access values that are in an object, inside an array.
Specifying the object you need with [0] because it's the first object in the array, will get the value. You would also need to do the same with members.
This should be what you need:
var jsonData = pm.response.json();
pm.environment.set("teams_id", jsonData.teams[0].id);
pm.environment.set("user_id", jsonData.teams[0].members[0].user.id);
Danny thanks for your response. I found another thread about parsing json data from an array and used this response to fix the issue. Here is what worked:
var jsonData = JSON.parse(responseBody);
for (var i = 0; i < jsonData.teams.length; i++) {
var teams_id = jsonData.teams[i];
console.log(teams_id.id);
console.log(teams_id.name);
postman.setEnvironmentVariable ("teams_id",teams_id.id)
postman.setEnvironmentVariable ("teams_name",teams_id.name)
}

JSON Array Parsing Kafka

I have the following setup:
A Rest endpoint to accept JSON POST request:
I am parsing the request and sending it as a String over Kafka.
But getting parsing errors
A Rest endpoint to accept JSON POST request:
[ { "Name": "Jack", "Id": "314", "Gender": "M" } , { "Name": "John", "Id": "451", "Gender": "M" }, { "Name": "Rita", "Id": "501", "Gender": "F" } ]
I am parsing the request as follows
#RequestMapping(method = RequestMethod.POST, value = "/record")
#ResponseBody
public String process(#RequestBody Map<String, Object>[] payload) throws
Exception {
String str = Arrays.toString(payload)
KafkaProd.toTopic(str);
System.out.println("Payload: " +str);
return "Record Processed";
}
str = Arrays.toString(payload) is changing it into the following format
[ { Name = Jack , Id = 314, Gender = M } , { Name = John, Id = 451,
Gender = M }, { Name = Rita, Id = 501, Gender = F } ]
When I'm trying to parse this string back into json array using json-s
imple :
JSONArray jsonArray = new JSONArray(record.value());
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println("Json Objects : "
+jsonArray.getJSONObject(i).toString());
}
I am getting JSON Parsing error, since the record.value() is not a valid json array
Option 1. How do I convert this to a valid json array?
Option 2. How do I send the json array in a proper format through kafka?
Which of these options do I use?
Instead of String str = Arrays.toString(payload) use a Jackson ObjectMapper to convert the map to a String.

How can I get a specific response value from response body in postman

{
"Group": "4r3rwee",
"EventType": "string",
"EventId": "string",
"Payload": "{\"Id\":\"6fd04f93e22e44c98752e209c1b74b03\",\"Name\":\"Md. Sakibur Rahman\",\"Email\":\"sakibur.rahmandd773661#orbitax.com \",\"Phone\":\"string\",\"Title\":\"Add Contact\",\"Status\":1,\"ContactType\":0,\"CompanyId\":\"automation\",\"ProjectId\":\"\"}",
"Status": 1,
"Id": "57c9c52a645a40f5bed0562dbee7d13b"
}
How can I get Id value from the payload?
I am using this command, but it's not working
pm.test("Set Contacts ID", function () {
var jsonData = pm.response.json();
//console.log("Response Payload : " + jsonData.Payload.{jsonData.Id});
pm.environment.set("contactIds", jsonData.Payload.Id);
});
As per the request data shown in the question, param. Payload contains JSON string. So, you'd need to parse it first, to access the property as follows,
pm.test("Set Contacts ID", function () {
var jsonData = pm.response.json();
var payloadData = JSON.parse(jsonData.Payload); //parsed payload
console.log(payloadData.Id);
pm.environment.set("contactIds", payloadData.Id);
});

Extract value from array of objects in Postman

I want to extract Id value from the array with objects in Postman and then set it as an environment variable. In case JSON response is an object, the following script works, but not with an array of objects (my array has only one object).
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("userid", data.Id);
JSON response:
[
{
"Id": 1287,
"LastName": "Trump",
"FirstName": "Donald",
"MiddleName": "Von",
"City": "New York City",
"Phone": "66 77 88",
"State": "New York",
"Fax": "111-222-333",
"ReferenceId": "12345",
"Active": false,
"CurrentWorkingSchemeId": null
}
]
If it is an array of objects, then just select the first object using index [0] before grabbing the object's key like this:
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("userid", data[0].Id);
This works like charm!
Basically what i am doing here is, parse the response and from the data array, take id and save it in postman environment variable.
var jsonData = JSON.parse(responseBody);
for (var i = 0; i < jsonData.data.length; i++) `
{
var counter = jsonData.data[i];
postman.setEnvironmentVariable("schID", counter.id);
}