Hi
Just an idea... it works, but haven't tested it fully.
This would take a bit of setup and (as described below) would only work if the metadata is shown on the frontend - as it utilises the %asset_frontend_metadata% keyword in the asset listing, together with an XSL file, and a combination of keyword replacements. It should incorporate any applied metadata schema changes dynamically (... as long as they are included on the frontend).
1. Create an asset listing that lists the asset name, id (or whatever else...) and front metadata (using %asset_frontend_metadata%) that is formated in xml.
See ouput example below:
Example output from the asset listing (the tag name could be anything):
Default Format:
<asset>
<asset_name>%asset_name%</asset_name>
<asset_id>%asset_assetid%</asset_id>
<fields>
%asset_frontend_metadata%
</fields>
</asset>
Listing output:
<metadata>
<asset>
<asset_name>pdf-converter</asset_name>
<asset_id>174785</asset_id>
<fields>
</fields>
</asset>
<asset>
<asset_name>test</asset_name>
<asset_id>261720</asset_id>
<fields>
<!-- Layout //-->
<meta name="Layout.Title" content="" />
<meta name="LayoutTitle2" content="" />
<meta name="ActualDate" content="2015-07-08 00:00:00" />
<meta name="TextDate" content="8-Jul-2015" />
</fields>
</asset>
<asset>
<asset_name>Test Page</asset_name>
<asset_id>272188</asset_id>
<fields>
<!-- Layout //-->
<meta name="Layout.Title" content="" />
<meta name="Layout.Title2" content="" />
</fields>
<asset>
</metadata>
2. Create xsl file that will format the xml output into JSON
Example below is based on the structure of the xml above. Note that empty curly braces have been used to end the loop. I use matrix to remove these and clean up the json - but there is probably a better way using the xslt formatting.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">[
<xsl:for-each select="metadata/asset">
{
"asset_name":"<xsl:value-of select="asset_name"/>",
"asset_id":"<xsl:value-of select="asset_id"/>",
"fields":[
<xsl:for-each select="fields/meta">
{"<xsl:value-of select="@name"/>":"<xsl:value-of select="@content"/>"},
</xsl:for-each>
{}]
},
</xsl:for-each>
{}]
</xsl:template>
</xsl:stylesheet>
3. Create an XSL asset in matrix with code such as above.
When adding as an xsl file in matrix - remove all the line breaks and unnessecary white space from the code above, and the output will be:
[{"asset_name":"pdf-converter","asset_id":"174785","fields":[{}]},{"asset_name":"test","asset_id":"261720","fields":[{"Layout.Title":""},{"Layout.Title2":""},{"ActualDate":"2015-07-08 00:00:00"},{"TextDate":"8-Jul-2015"},{}]},{"asset_name":"Test Page","asset_id":"272188","fields":[{"Layout.Title":""},{"Layout.Title2":""},{}]},{}]
4. Apply the xslt to the xml output by the listing
1234 = asset listing ID
5678 = xsl file ID
The 'replace:(,{}):' removes the empty objects added in the xslt formatting
%globals_asset_contents_raw:1234^xslt:5678^stripdecl^replace:(,{}):%
Thanks
Peter