Find nested variable inside json payload - postman

I'm using the find function in postman tests to save environment variables,
the find function works great when I'm looking for a variable, But I cannot get it to work when looking for variables inside an object
My payload looks something like this
{
"name": "product1",
"state": {
"DefinitionId": "productcard",
"Id": "32919b8c-984e-46c3-933d-51d3c621d4cf"
},
"status": "Done"
},
{
"name": "product2",
"state": {
"DefinitionId": "productaccount",
"Id": "4999b8c-984e-46c3-933d-55d3c621d4cf"
},
"status": "NotDone"
},
with the _find function I can find variables through the name variable
var steps = _.find(resBody, {
name: "product1",
})
pm.environment.set(steps.name, steps.state.Id);
But what If I want to search by DefinitionId?
I have tried stuffs like this
_.find(resBody, {
name.state: "product1",
}) <--did not work
_.find(resBody.state, {
name.state: "product1",
}) <--did not work [returns object object]
Thanks in advance.

This should return the an object by a DefinitionId search, if that's what you mean?
var result = resBody.find(x => x.state.DefinitionId === "productaccount");

Related

Wy do I get an error when issuing QueryCommand in nodejs sdk v3?

I am using aws node.js sdk v3. I want to do a simple query on the table using dynamodbdocumentclient. I wanted to do just a test if query works on a simple music table which is provided by aws developer guide.
const data = await this.dynamoDBDocumentClient.send(new QueryCommand({
TableName: "Music",
KeyConditionExpression: "#Artist = :artist",
ExpressionAttributeName: {
"#Artist": "artist"
},
ExpressionAttributeValues: {
":artist": "Acme Band"
},
});
But when I call this I get:
TypeError: Cannot read property '0' of undefined
When I do a simple:
const data = await.this.dynamoDBDocumentClient.send(newScanCommand({TableName: "Music"});
I am getting the correct response:
{
"AlbumTitle": {
"S": "Somewhat Famous"
},
"Awards": {
"N": "1"
},
"Artist": {
"S": "No One You Know"
},
"SongTitle": {
"S": "Call Me Today"
}
},
{
"AlbumTitle": {
"S": "Songs About Life"
},
"Awards": {
"N": "10"
},
"Artist": {
"S": "Acme Band"
},
"SongTitle": {
"S": "Happy Day"
}
},
{
"AlbumTitle": {
"S": "Songs About Sadness"
},
"Awards": {
"N": "8"
},
"Artist": {
"S": "Acme Band"
},
"SongTitle": {
"S": "Sad Day"
}
}
],
"ScannedCount": 3
}```
What am I doing wrong? I don't get why I am getting en error.
I just had the same problem, and according to this guide, the ExpressionAttributeValues must follow the DynamoDB JSON schema, being so, if your artist is a string, it should be:
...
ExpressionAttributeValues: {
":artist": {S: "Acme Band"}
},
...
Adding this, worked for me.
I think you simply have a typo here:
ExpressionAttributeName
It should be ExpressionAttributeNames (missing last letter).
However AWS-SDK is not very helpful with error messages. Use TypeScript to avoid similar issues, since it will show you, that you are using wrong field name basing on types definition.

Postman - Failing to get value from an object that has `:` in the property key

I'm struggling to get value from an object that has : within the key name of property.
This is how my Response Body looks:
{
"links": {
"content": {
"href": "http://*********",
"templated": false,
"type": "application/hal+json"
},
"test:search": [
{
"title": "Some title",
"href": "http://*************",
"type": "application/hal+json"
}
]
}
}
When I try to get the href value from test:search, Postman is giving me an error:
"Missing ";" before statement".
This should work if you add it to the Tests tab:
_.each(pm.response.json().links['test:search'], (item) => {
console.log(item.href)
pm.environment.set('href', item.href)
})
If you were to use this to reference the property pm.response.json().links.test:search it would fail to set the variable.

How to iterate and get the properties and values of a JSONAPI response in emberJS?

I have following ember request
this.store.createRecord('food_list',requestObj
).save().then((response) => {
console.log(response);
console.log(response.id); // This is working
console.log(response.food_list_code); //this does NOT work !!!!!
}
It will call an API and save a record to database and then returns following response.
{
"links": {
"self": "/api/food_list"
},
"data": {
"type": "",
"id": "da6b8615-3f4334-550544442",
"attributes": {
"food_list_date": "2013-02-14 23:35:19",
"food_list_id": "da6b8615-3f4334-550544442",
"food_list_code": "GORMA",
},
"relationships": {
"food_list_parameters": {
"data": [
{
"type": "food_list_parameter",
"id": "RERAFFASD9ASD09ASDFA0SDFASD"
}
]
},
"food_new_Name": {
"data": {
"type": "food_new_Name",
"id": "AKASDJFALSKDFKLSDF23W32KJ2L23"
}
}
},
"links": {
"self": "/api/BLAH/BLAH/BLAH"
}
}
}
but since above response is a JSONAPI in form of an ember object, I dont know how to parse it.
If I try to get response.id, I get the string da6b8615-3f4334-550544442
But how to get value for food_list_code in response block. Or how to iterate the response object to get "food_list_code" and "food_list_date" ?
The output for console.log(response) is as following ember class
Class {__ember1500143184544: "ember1198", store: Class, _internalModel: InternalModel, currentState...
I appreciate your help.
M.

Search Array and Set Environment Variables

My Postman response body is this:
{
"jobs": [
{
"id": "00d21be0",
"name": "IT Department"
},
{
"id": "h27da349",
"name": "Car Sales"
},
{
"id": "5d2db4125",
"name": "Grocery Clerk"
},
{
"id": "65cd0cc1d",
"name": "Accounting Department"
},
{
"id": "8462284587",
"name": "Nurse"
},
{
"id": "9fe2ff9ee4",
"name": "Astronaut"
},
{
"id": "f40cb44799",
"name": "Phone Operator"
},
{
"id": "f4e0483257",
"name": "Project Leader"
}
]
}
What I would like to do is parse this response and set an Environmental Variable for id's associated with Nurse, Astronaut, and Grocery Clerk. The rest of the info I do not need. I cannot use the array [number] because its not a guarantee they come in the same order on other systems.
Is JSON.stringify response body the way to go? How do I pull those values?
My Postman test so far is:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentalVariable("jobs", JSON.stringify(jsonData));
What you can do is parse your json body with a loop and, when you hit the names you want, you set a global variable with the corresponding id, like :
// get the response body
var jsonData = JSON.parse(responseBody);
// init counter
var loop_count = 0
for (count = 0; count < jsonData.jobs.length; count++)
{
if (jsonData.jobs[count].name == "Nurse")
{
var job_id = jsonData.jobs[count].id;
postman.setEnvironmentalVariable("env_nurse", job_id);
}
}
This is the principle.
You can do if/else for the different names you want to catch but I don't recommend it (use an array that you can expand easily).
Same thing for the environment variable name, instead of hard coding, try to generate it with respect to your element name.

AlpacaJS: programmatically change value of TextField after selecting Select

I'm new to AlpacaJS and getting crazy trying to figure out how to do a simple stuff like changing dinamically the content of a text field with the value of a "Select".
The code looks like
$("#form1").alpaca({
"data": {
"name": "Default"
},
"schema": {
"title": "What do you think of Alpaca?",
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"flavour":{
"type": "select",
"title": "Flavour",
"enum": ["vanilla", "chocolate", "coffee", "strawberry", "mint"]
}
}
},
"options": {
"helper": "Tell us what you think about Alpaca!",
"flavour": {
"type": "select",
"helper": "Select your flavour.",
"optionLabels": ["Vanilla", "Chocolate", "Coffee", "Strawberry", "Mint"]
}
}
},
"postRender": function(control) {
var flavour = control.childrenByPropertyId["flavour"];
var name = control.childrenByPropertyId["name"];
name.subscribe(flavour, function(val) {
alert("Val = " + val);
this.schema.data = val;
this.refresh();
});
}
});
I can see that the function in postRenderer is called (as I can see the alert with relevant value) but (maybe I'm brain dead at this stage) I cannot refresh the text field with that value.
Cheers
I was probably looking at the wrong attribute to be set... After I changed to
this.schema.data = val;
it worked fine :)