Hey,
I am trying to fetch a value inside a JSON structure and I cannot get it right. Maybe this community can give me a hint how to write my smart value variables for this use case
Background: I have read up a bunch of Assets objects via the Assets REST API end point: /object/aql. The webResponse is an array of objects under the "values" tag.
I create a branch from the {{webResponse.body.values}} smart value and call the branch variable myObject. So far so good. In each object structure in the array there is an attributes-array called attributes. In this array I need to get the value for an attribute with a specific attribute-id.
The structure looks like this (simplified):
{
"values": [
{
"id": "20",
"attributes": [
{
"objectTypeAttributeId": "4839",
"objectAttributeValues": [
{
"displayValue": "Some attribute value"
}
]
},
{
"objectTypeAttributeId": "4853",
"objectAttributeValues": [
{
"displayValue": "This is my value to fetch"
}
]
}
]
},
{
"id": "21",
--- the next object ---
In my branch I create a variable to get the attribute array:
varAttributes --> {{myObject.attributes}}
This works fine. But I can't get down to the requested attribute-id from here. I would like to create a variable that gives me the value "This is my value to fetch", i.e. the value from the attribute with ID "4853".
I have tried a number of variants of setting a new variable to something like: {{#varAttributes}}{{.}}{{/}} and some if-clause that tests for the ID value, but I can't get it right.
EDIT Oct, 1st:
I am still struggling with this and I am now approaching it using @Bill Sheboy solution with Dynamic list searching using a regular expression. The issue I am facing now is that I can't iterate over the field-array: attributes.
I have my variable varAttributes as described, but it does not seem to be a list I can iterate over and use dot-notation. This is what I need to do
{{#varAttributes}}{{objectTypeAttributeId}}:{{.}}~~{{/}}
But it seems it is not a list that I can iterate like this. Is it possible to "convert" it to such a list?
Looking forward to hear back from you!
Cheers,
// Svante
Hi @Svante Gustafsson Björkegren
Perhaps the nested structure is recognized as text rather than as an object.
You could test that by using jsonStringToObject() on your branch variable, and then trying to access each attribute:
Kind regards,
Bill
Hey
I did try this but the jsonStringToObject()-function throws an error like this:
The weird thing is that the sub-array: varAttributes is not in JSON string format what I can see. It seems to already be in JSON object format, i.e. the same format as the original webResponse. Checkout the log:
But when I try to iterate over it using:
{{#debug}}{{#varAttributes}}{{id}}{{/}}{{/}}
I get nothing, but when I replace the {{id}} with {{.}} I get the entire array. That tells me that there is only one "object" in the variable.
It feels that the textual format is correct but the automation is not treating it as a proper format that works with the dot-notation.
Do you have any idea if we can cast this in some way to tell the automation to treat it differently?
Cheers,
// Svante
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That last bit could also indicate it is text and not JSON...
The audit log writes text only, and so uses some equivalent of __toString() function when trying to log values. Your test trying to iterate over {{varAttributes}} indicates it is a single text value.
And, also please note: I recently learned the {{#debug}} ... {{/}} syntax cannot be used in the source, smart value for an Advanced Branch. It appears the act of logging collapses it all to a single text value. If you have a debug in the branch source, please try removing that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Svante Gustafsson Björkegren
What if you would simply do: {{webResponse.body.values.attributes.objectTypeAttributeId}}
Do you get all the ID's then?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, that will give me the attribute-ids but I need to fetch another field in the structure where the id=4853
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.