JSON.parse with dynamic parameters within keyword doesn't working


Matrix Version:

Hello, I have problem with parsing dynamic keyword.

<script type="text/javascript" runat="server">
    var assetId = 56234;
    var data = "%globals_asset_children:" + assetId + "^index:0^as_asset:asset_metadata_json%";

Getting this error:
File: [SYSTEM_ROOT]/fudge/general/text.inc Line: 2199
Message: Error occured when processing server side JS: undefined:1: SyntaxError: Unexpected token % - ‘%globals_asset_children:56234^index:0^as_asset:asset_metadata_json%’ Line: 1

(Nic Hubbard) #2

Are you sure that data returns valid JSON?

(John gill) #3

I don’t think this can ever work, because the global keywords and the javascript are processed at different times.


You either need the Keyword Replacements to be processed before the javascript runs, or you need the javascript to produce a valid Keyword Replacement that can be processed after the javscript has finished. You can’t assemble a Keyword Replacement with javascript, have it processed, and then use the result in more javascript.

Also, as far as I know SSJS is “incomplete” in, all the documentation is 5.4 based. Some of it works, but some doesn’t. I suspect trying to use SSJS before you have 5.4 is one of those “you’re gonna have a bad time” situations.


Sorry, I made a mistake in the version, we are using I am using SSJS in paint layout which is applied to JSON, so I need to use print from SSJS. Is there any solution or work around?

(John gill) #5

No, I think what you’re trying to do is impossible regardless of version. You need to think of Keyword Replacements as things that happen either before your SSJS runs, or after your SSJS runs (using evalkeywords=“post”).

(Bart Banda) #6

As John said, that format won’t work as they are processed at different times. However, can you share a bit more info on what you are trying to do? And where the dynamic asset ID is coming from? Something like that is still achievable, but all depends on the full context.


Hello Bart, I am creating assets in which data is saved in one metadata field as object. Asset has applied paint layout which converts this object to JSON output. Paint layout is mapping through the object and replacing some asset id placed in object property to metadata from this asset. Then I would like to do some action on this object but I cannot parse it. May be there is some way to run first paint layout then second.

The first post was an example.

(Bart Banda) #8

Still not 100% on what you mean, but essentially doing something like this:

var data = "%globals_asset_children:" + assetId + "^index:0^as_asset:asset_metadata_json%";

Won’t work as Matrix will try and parse that as a keyword. In order for that to work, you’d need to do something like this:

var data = "%" + "globals_asset_children:" + assetId + "^index:0^as_asset:asset_metadata_json%";

But again, that keyword will not be replaced until after the SSJS is rendered, which is probably not what you want.

Where is the assetId value coming from? How is it sourced?


I am using form with different kind of input fields which are saved in metadata fields. Creating assets is done through the use of JS API.

Edit: Solution with splitting “%” doesn’t working. Is there different way to concatenate keyword?


Hey Damian,
Did find a way of doing this?
I am doing something similar, trying to use results of asset_children to construct new keywords.