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

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.

Related

Find nested variable inside json payload

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");

How do you handle ProgressPercentage not returning 100?

I am using the node #aws-sdk/client-timestream-query 3.53.0 package. I am running into an issue where the result of ProgressPercentage is not 100 but the promise returned.
const promise = this.client
.send(command)
.then((data) => parse(data))
.catch((err) => err);
this.cache.set('accountPlatforms', promise);
return (await this.cache.get('accountPlatforms')) || []
Then inconsistently we will get results that return like this from the promise.
{
"$metadata": {
"attempts": 1,
"httpStatusCode": 200,
"requestId": "redacted",
"totalRetryDelay": 0
},
"ColumnInfo": [
{
"Name": "platform",
"Type": [
null
]
},
{
"Name": "success",
"Type": [
null
]
},
{
"Name": "failure",
"Type": [
null
]
},
{
"Name": "total",
"Type": [
null
]
}
],
"NextToken": "redacted",
"QueryId": "redacted",
"QueryStatus": {
"CumulativeBytesMetered": 10000000,
"CumulativeBytesScanned": 108896,
"ProgressPercentage": 67.63129689174706
},
"Rows": [
]
}
I don't see a way to look up the completed query either by requestId or queryId from inside the our service.
Anyone know how to get the completed query?
a progress percentage < 100 means that the query is not yet done. This is returned from Amazon Timestream in case the query runs longer than the defined timeout. Whenever you see this, you should expect that rows[] is empty but you get a NextToken.
You can poll with next token as described here:
https://docs.aws.amazon.com/timestream/latest/developerguide/code-samples.run-query.html
You need to use the "NextToken" from the first query result you obtain and use it to make a new query using it, if the ProgressPercentage<100.
You will then have the result.
This is the way i do.
Like in Python i do:
if query['QueryStatus']['ProgressPercentage']<100:
next_token = query['NextToken']
query = timestream_query_client.query(QueryString=queryString, NextToken=next_token)
Hope it helps also after time or who is coming next having the same problem.

JSON Schema if/then/else for property that can be an object or null based on another properties value

I have a property that will be an object or null based on the value of another property. I'm trying to add this new check to my schema using an if/then/else. This is for AJV validation in Postman if that's pertinent .
For example, this sample payload
{
"topObj": {
"subItem1": "2021-09-12",
"subItem2": "2021-09-21",
"ineligibleReason": "",
"myObject": {
"subObject1": true,
"subObject2": ""
}
}
}
If ineligibleReason is an empty string then subObject should be an object. If ineligibleReason isn't an empty string then subObject should be null as in this table:
ineligibleReason value
myObject value
schema valid?
""
object
true
""
null
false
"any value"
null
true
"any value"
object
false
Here's what I have so far. jsonschema.dev thinks it's a valid schema but when I add a value to ineligibleReason in the payload (keeping myObject as an object) it still says the JSON payload is valid!
{
"type": "object",
"properties": {
"topObj": {
"type": "object",
"properties": {
"subItem1": { "type": "string" },
"subItem2": { "type": "string" },
"ineligibleReason": { "type": "string" },
"myObject": { "type": ["object", "null"] }
}
}
},
"required": ["topObj"],
"additionalProperties": false,
"if": {
"properties": { "topObj.ineligibleReason": { "const": "" } }
},
"then": {
"properties": {
"topObj.myObject": {
"type": "object",
"properties": {
"subObject1": { "type": "boolean" },
"subObject2": { "type": "string" }
},
"required": ["subObject1", "subObject2"],
"additionalProperties": false
}
}
},
"else" : {
"properties": { "myObject": { "type": "null" } }
}
}
I have this in jsonschema.dev but it gives Schema Error "/properties/required" should be object,boolean.
My basic schemas are working but I'm not sure how to add this conditional validation based on another properties value.
Update 1 I updated the schema and it's now parses as valid. However the payload validates when ineligibleReason has a value and myObject is an object instead of being null.
Update 2 I updated the schema again, moving the if/then/else to the bottom (no longer "inline"). The schema definition parses as valid, however the payload validates successfully irrespective of the invalid situations (ineligibleReason has a value and myObject is an object instead of being null).
How do I get the if/then/else to validate my subObject property correctly?
The error message is pointing to the problem. /properties/required is declaring a property named "required", and then the value under that needs to be a schema (object, or boolean). So you need to lift that "required" to be adjacent to "properties", rather than beneath it.
Answer per Ryan Miller on json-schema Slack. A slightly different tack then I was trying but simpler and works!
{
"type": "object",
"properties": {
"topObj": {
"type": "object",
"properties": {
"subItem1": { "type": "string" },
"subItem2": { "type": "string" },
"ineligibleReason": { "type": "string" },
"myObject": {
"type": ["object", "null"],
"$comment": "'properties', 'required', and 'additionalProperties' only make assertions when the instance is an object.",
"properties": {
"subObject1": { "type": "boolean" },
"subObject2": { "type": "string" }
},
"required": ["subObject1", "subObject2"],
"additionalProperties": false
}
},
"required": ["subItem1", "subItem2", "ineligibleReason", "myObject"],
"additionalProperties": false,
"if": {
"$comment": "Is an ineligibleReason defined?",
"properties": {
"ineligibleReason": {"minLength": 1}
}
},
"then": {
"$comment": "Then 'myObject' must be null.",
"properties": {
"myObject": {"type": "null"}
}
}
}
},
"required": ["topObj"],
"additionalProperties": false
}

Send a function along with the scheme from backend in Alpacajs

I am having an API in backend to return the full json (schema and options) for a AlpacaJS form. The content-type of the response is application/json. Following is a sample response,
{
"options": {
"fields": {
"students": {
"items": {
"type": "tablerow"
},
"type": "table"
}
},
"form": {
"buttons": {
"submit": {
"click": "function(){alert(\"sample\");}"
}
}
}
},
"schema": {
"properties": {
"students": {
"items": {
"properties": {
"name": {
"required": true,
"title": "Name",
"type": "string"
},
"contact-number": {
"title": "Age",
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
}
When I click on the Submit button, I get the following error in browser console,
Uncaught TypeError: t.call is not a function
I think the issue is that the function is considered as a string in the following section of the response.
"form": {
"buttons": {
"submit": {
"click": "function(){alert(\"sample\");}"
}
}
}
Is there a way in AlpacaJS to send a javascript function from backend, or is there a way to convert the function string to a javascript function in frontend?
In order to get that, you should transform the stringified function to a function by doing new Function('return ' + val)(); (beware this is a form of eval and eval is evil).
Here's a working fiddle for that.
Tell me if it didn't work for you.

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.