My understanding is that %frontend_asset_data_metadata^empty:{}% should not contain newlines even if the asset has metadata fields which contain newlines.
I think there’s a false premise or missing info here somewhere. Tests on 5.5.6 and 6.18 indicate that asset_data_metadata is safe to use directly in SSJS[1] even if it contains metadata fields that contain new lines. This fits with the documentation of the keyword which says it returns “A JSON object of the asset’s metadata fields and values.”
In the absence of enough detail to replicate the issue, any speculation on resolutions risks becoming misinformation for people searching the forum in the future.
@gboyd are you able to share more detail on exactly what you’re seeing from that keyword, and where the spaces are coming from?
1: (you don’t even need ^empty:{}, because a page with no MD fields will return an empty array that will then be turned into [] by the implicit as_json)
%asset_metadata_x% keywords always return a string, never an object by default, so I’d expect using ^json_encode will make it work. Because at the end of the day, metadata values are just stored as text strings in the DB, and that’s what gets loaded by default.