Is asset_parents an array?


(Andrew Harris) #1

Hi there, I'm puzzled by the keyword %asset_parents%
 
It appears to return an array: ["1474618","1451012","1464398"], but modifiers which are supposed to work with arrays have no effect. eg: %asset_parents^count%

 

Ultimately, I'd like to return attributes, such as the URLs, of the various parents of my asset. I just thought the first step would be to manipulate the array, but I can't even get past that!

 

Maybe there is a better way?

 

v5.3.4.0


(Peter McLeod) #2

Hi

 

The array specific modifiers seem to work as expected when I use it on asset parents (also on v5.3.4.0). What gets output when you use the count modifier?

 

You could just pass the %asset_parents% keyword to a nested asset listing that is set to replace the root node with an array of ids. Might be the simplest option.

 

Not sure if you can iterate over or get multiple replacement values from an array using keywords, a listing might be easier. 

 

You could get individual replacement values using something like:

 

%asset_parents^index:0^as_asset:asset_url%

 

Thanks

Peter


(Andrew Harris) #3

Thanks for the response, Peter, however, it only deepens the mystery for me!

 

This is what I'm seeing: (keyword = output).

 

%asset_parents% = ["2026668","1451012"]
%asset_parents^count% = ["2026668","1451012"]
%asset_parents^index:0% = ["2026668","1451012"]
 
Time to escalate, methinks!

(Bart Banda) #4

%asset_parents% will return an array string, not an array object.

 

To use array like modifiers on it you need to first use the ^json_decode modifier, so for example:

 

%asset_parents^json_decode^index:0%


(Andrew Harris) #5

Whoah!

 

Thanks Bart, but this is not exactly my experience - in fact, I think I've worked it out, but it's kinda blown my mind.

 

For background. this is all happening in the type format of an asset listing.

 

My first use of the keyword is like this: %asset_parents^index:0^as_asset:asset_name_linked%, and it works fine as an array without conversion, and I continue using it as an array for a number of different operations in a little code block.

 

In a second block of code, I try to use %begin_asset_parents^contains:2062797% and it fails - presumably, because it's returning an array and I'm trying to run a string operation.

 

So, I threw in the raw keyword again, just to help with debugging, but it got the second block working, and now the first code block failed! It was like I'd switched the state of the keyword from array to string, simply by calling it again.

 

Not to be defeated, I threw in a modifier which would return a string: %begin_asset_parents^as_csv_noheader^contains:2062797% and it worked - without breaking the first code block!

 

The lesson seems to be that the %asset_parents% keyword will work either as an array, or a string, depending on how you first use it, but that once you've used it one way, you can't use it the other.

 

I hope this is useful, because I've just wasted hours learning the lesson!! :-)

 

Cheers,

Andrew H


(Peter McLeod) #6

Hi Bart 

 

The reverse seems to be case for us on one specific instance of matrix., eg %asset_parents^count% will return an int matching that length of the array,

 

On different instance (same version 5.3.4.0.) that we use - it works as you described above ie. returning an array string.

 

Is there any reason why this would be happening?

 

Thanks

Peter


(Andrew Harris) #7

Oh, and thanks again Bart, because your hint about converting the state was the clue I needed! :-)


(Peter McLeod) #8

Thanks Andrew 

That would probably explain the variation because difference instances I mentioned above.

Peter


(Bart Banda) #9

Ah sorry guys, the %asset_parents% does return the array properly, I was thinking more in those instances where arrays are returned as strings. It can be tricky as sometimes you don't know.

 

For example, if you put %asset_parents% in a metadata field and then reference that metadata value, you would need to use the ^json_decode as the value you are accessing is a string (metadata text data). 

 

 

On different instance (same version 5.3.4.0.) that we use - it works as you described above ie. returning an array string.

 

What's the instance/example?


(Peter McLeod) #10
Hi Bart - my mistake.
 
The asset I was testing only had 1 parent in the array:
 
Using the %asset_parents^json_decode^index:0% modifer printed out the array: ["732258"] - which I misstook for as being correct as it only has one item.
 
Using %asset_parents^index:0% - correctly returns 732258.
 
Sorry for the confusion.
Peter