Postman: how to get count of number string value from response - postman

I want to get the count of string name from the response, ex: here my count in 2
[
{
"name": One,
"next": null,
}
{
"name": Two,
"next": null,
}
]

If that's your response data, you could use:
console.log(_.filter(pm.response.json(), 'name').length)
This would console log out the number of keys that are called name.

Related

AWS Step Functions - Choice

Given this input (which is output from a previous map task):
[
{
"result": {
"validated": true,
"order": "1"
}
},
{
"result": {
"validated": true,
"order": "2"
}
}
]
how do I access the validated flag in a choice task. I would have thought it is:
"Choice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.result[0].validated",
"BooleanEquals": true,
"Next": "Success"
}
],
but that doesn't work - I get "Invalid path '$.result[0].validated': The choice state's condition path references an invalid value".
Also I will never know how many 'results' will be in that array (produced by the map task). How can I process the 'validated' flag to check that they are all true? Any suggestions welcome!
hi with the given input it is evident that '$.result[0].validated' will be invalid as '$' represents the root of the input and you are accessing 'result' on the root which does not exist. '$[0].result.validated' should work instead.

AWS State Machine - Update DynamoDB table is replacing the ID with "$.id"

I have a step where I want to update a object on a DynamoDB table.
Everything works except its creating a new object with the ID value of "$.id", instead of updating where the ID I pass in.
This is my first state machine attempt so what have I done wrong here?
"update-table-processing": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:updateItem",
"ResultPath": "$.updateResult",
"Parameters": {
"TableName": "Projects",
"Key": {
"id": {
"S": "$.id"
}
},
"UpdateExpression": "SET step = :updateRef",
"ExpressionAttributeValues": {
":updateRef": {
"S": "processing"
}
},
"ReturnValues": "ALL_NEW"
},
"Next": "create-project"
},
Do I somehow need to tell DynamoDB to evaluate "$.id" rather than treating it as a "S", or is this happening because I've not mapped the input correctly that the "$.id" value is empty?
My input looks like:
{
"id": "f8185735-c90d-4d4e-8689-cec68a48b1bc"
}
In order to specify data from your input you have to use a Key-Value pair, with the key value ending in a ".$". So to fix this you need to change it to:
"Key": {
"id": {
"S.$": "$.id"
}
},
Using the above it should correctly resolve to the value from your input instead of the string value "$.id".
References - https://docs.aws.amazon.com/step-functions/latest/dg/input-output-inputpath-params.html#input-output-parameters

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.

Is it possible to suppress fields from API results?

For example, I wish to get the like count for each post without getting name and Id details of each like.
The likes attribute returned for a call to me/posts?fields=likes.summary(true) would be:
"likes": {
"data": [
{
"id": "aaa",
"name": "bbb"
},
{
"id": "ccc",
"name": "ddd"
},
...
...
],
"summary": {
"total_count": 56,
"can_like": true,
"has_liked": false
}
}
It's the "data" attribute I'd like to suppress coming back due to it (and the same for comments and other fields I'd like to turn off) substantially increasing the bandwidth of data received.
It's the "data" attribute I'd like to suppress coming back due to it (and the same for comments and other fields I'd like to turn off) substantially increasing the bandwidth of data received.
Specify a limit of 0, then you will just get an empty data structure:
me/posts?fields=likes.summary(true).limit(0)
If you enumerate the post id's, you can then get the summary of likes for each post by calling:
/<post-id>/likes?summary=true&fields=total_count
This returns something like:
{
"data": [
{
"id": "<id>"
}
],
"paging": {
"cursors": {
"before": "...",
"after": "..."
}
},
"summary": {
"total_count": 3,
"can_like": true,
"has_liked": false
}
}

how to remove outer array in django rest json response in readonlymodelview

I getting data in array like
[
{
"category_id": "Glass_Door_Handle",
"category_name": "Glass Door Handle",
"product_name": [
{
"product_id": "SP-001",
"name": "RENUALT-SOLID-MD",
"image": "http://127.0.0.1:8000/media/1-1_aIzfcnG.jpg",
"size": [
"http://127.0.0.1:8000/api/sizemattcp/7/"
],
"timestamp": "2016-01-14T05:33:44.107117Z",
"updated": "2016-01-14T05:33:44.107142Z"
}
]
}
]
I want to data in
{
"category_id": "Glass_Door_Handle",
"category_name": "Glass Door Handle",
"product_name": [
{
"product_id": "SP-001",
"name": "RENUALT-SOLID-MD",
}
]
}
I am using readonlyViewModel
It seems you are calling your api like:
/api/models/?filter=value
And it returns your a list of objects, which contains only one element. To get a single object, just append its primary key to the url:
/api/models/1234/
If you want to get models not by id but by some other field, use the ViewSet.lookup_field parameter to specify the name of that field.