Metadata to JSON


(Neil Dakeyne) #1

Hello,

 

First post! I've been using Matrix for a number of years now but only just found this forum.

 

I'm looking for a way to output the metadata associated with a bunch of assets as JSON. I've achieved this (to an extent) with an asset listing but this will require me to go back and update it every time we add an item to the schema. I've also had to use position formats to remove the comma from the final result.

 

Is there a more efficient way to generate JSON from the metadata? I'm on Matrix 5.1.

 

Thanks

 

 

 

 

 

 

 


(Nic Hubbard) #2

I feel your pain, and have done exactly what you have done. Also looking/wish there was a better way, but I don't think there is...


(Peter McLeod) #3

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


(Neil Dakeyne) #4

Thanks for such a detailed reply Peter, I'll give it a try later.

 

I don't particutly want to expose all that extra data onto the front end but if there is no other way around I might have to.

 

Thanks again.


(Joel Porgand) #5

What are you actually trying to do? The getMetadata Javascript API call will give you the metadata for an asset in JSON format, and you can batch the request. You could possibly do some jiggery-pokery with a rest resource javascript asset on top of that if necessary. 


(Neil Dakeyne) #6

What are you actually trying to do? The getMetadata Javascript API call will give you the metadata for an asset in JSON format, and you can batch the request. You could possibly do some jiggery-pokery with a rest resource javascript asset on top of that if necessary. 

 

J.P - I have a whole load of events that people can sign up for (some with differing requirements) so I'd like to be able to pass that information into a single page which will then generate the form relevent to the event.

 

I'd also like the JSON data to be availble so I can create maps and 'events near you' type features.

 

I haven't had much experience with the JS API so it hadn't crossed my mind to use it. I'll take a look later.

 

Thanks.


(Alderman) #7

What are you actually trying to do? The getMetadata Javascript API call will give you the metadata for an asset in JSON format, and you can batch the request. You could possibly do some jiggery-pokery with a rest resource javascript asset on top of that if necessary. 

 

I need to expose a bunch of data records with associated metadata fields as a JSON feed for a separate corporate system to consume.  How would the batching work using the API call mentioned above?  If I just used an asset listing would I be able to surround the looped items with the JSON structure (in the Default Format instead of using the API)?

 

*** UPDATE **

 

Actually I can see we have already done this kind of thing - cheers.