JSON Data Attributes


(Kieran) #1

Hi guys,

I always have trouble with JSON Data Attributes using JSON Data Source.

If the JSON is:

{"City":{ "Brisbane":{ "things":[ {"key1":"ABC", "key2":0}, {"key1":"ABC","key2":0} ]}, "Sydney":{ "things":[ {"key1":"ABC", "key2":0}, {"key1":"ABC","key2":0} ]} }}

or

{"City":[{ "Brisbane":[{ "things":[ {"key1":"ABC","key2":0}, {"key1":"ABC","key2":0} }] }], "Sydney":[{ "things":[ {"key1":"ABC","key2":0}, {"key1":"ABC","key2":0} ] }] }]}

And I set the Root Object as “City”, I get “Brisbane” and “Sydney” as available keywords.
If I enter “City.Brisbane.things” as a JSON Data Attribute, I dont get any extra options. Same goes for “City.Brisbane.things.key1”.

Can someone please help me get all available keywords?


(John gill) #2

Disclaimer: not the answer you asked for

The addition of SSJS has really tipped the scales to the point where the JSON Data Source is more hassle than it’s worth. Pre-SSJS it provided functionality you couldn’t get any other way, but as far as I can tell any use case for those is better handled with SSJS.

<script runat="server">
var data = %globals_asset_file_contents:12345%;
Object.keys(data.City).forEach(city => print(`<p>${city}</p>`);
</script>

Either manually iterating over the data, or using a template library like Mustache, for my money this is the “correct” way to deal with JSON in Matrix and JSON Data Source should be considered defunct.

SSJS is especially good if your JSON data’s schema is not under your control, because you can work with arbitrary structures in JS that would cause problems with the JSON Data Source.


(Kieran) #3

Thank you John,

Any reason why %globals_asset_file_contents:6496% would be empty?
Previewing the asset (REST Resource) definitely has an output.

<script runat="server">
var auctionData = "%globals_asset_file_contents:6496%";
Object.keys(auctionData.City).forEach(city => print(`<p>${city}</p>`));
</script>

Edit: with and without the string quote marks around the keyword still doesnt get an output. Keyword by itself doesnt either.


(Kieran) #4

Nevermind. It’s because it wasnt a file. It was a REST Resource.
I didnt pick up on the globals_asset_file_contents


(Kieran) #5

Returning to say thank you, John.
I needed to do much more complicated things with the array/objects, so I didnt use your code, but you put me into SSJS and I eventually got there.
Built it in JS and then ported it across to SSJS and changed methods where needed.

Thanks so much!