Metadata Schema value as JSON data


#1

Hi.
I am trying to export squiz metadata schema information as JSON data.

I am able to get an output for asset_id, I am also able to get output for the schema name to a point, but not the value of the schema.

code:

 const squizschema =%globals_asset_children:####^as_asset:asset_name,asset_assetid,asset_url,asset_published_short,asset_data_metadata%;
let squizKeys = squizschema.map(
    obj => {
        return {
            "id" : obj.asset_assetid,
            "metadatavalue" : obj.asset_data_metadata.metadata_heading.value,

        }
    }
);
const provideOutput = (function(asset) {
          print(JSON.stringify(squizKeys));
});
provideOutput();
</script>

If I change:

"metadatavalue":obj.asset_data_metadata.metadata_heading.value,

to

"metadatavalue":obj.asset_data_metadata.metadata_heading,

I am getting an output. But it gives me what I want, and then a whole lot of what I dont.

  {value: "This is what I want", ,"fieldid":"####","type":"metadata_field_text","is_contextable":true,"default_value":true,"use_default":true}

How am I able to modify to give the preferred output below?:

{"metadatavalue":"This is what I want"}

Thank you.


Streamlining process to get metadata from multiple folders - Squiz/JS question
(John gill) #2

I’m not entirely clear on what you’re trying to do, but I think there are two things at play

  • %asset_data_metadata% contains a lot more than just key:value
  • since you’re using %asset_children whatever you do to transform the metadata bit will need to be done per-child

To reformat the asset_data_metadata object to something simpler I’d use Object.entries to change it from {key:value} to [[key,value]], then run .map over it to reduce the value down to just the bit I want, then Object.fromEntries to change it back to an object

Here’s an example that uses the same keyword as yours, prints out the original and then the cut-down version that I think you’re after.

<pre><script runat="server">
    var list = %globals_asset_children:4033^as_asset:asset_name,asset_assetid,asset_url,asset_published_short,asset_data_metadata%;
    print('---Original---\n\n')
    print(JSON.stringify(list,null,2));
    

    print('\n\n---Cut down---\n\n')
    
    list.forEach(child => {
        child.asset_data_metadata = Object.fromEntries(Object.entries(child.asset_data_metadata).map(k => [k[0],k[1].value]));
    })
    print(JSON.stringify(list,null,2));
</script></pre>

which spits out

---Original---

[
  {
    "asset_name": "Test",
    "asset_assetid": "4029",
    "asset_url": "http://matrix.local/forum13132/listfolder/test",
    "asset_published_short": "1970-01-01",
    "asset_data_metadata": {
      "Related": {
        "value": "4022",
        "fieldid": "4024",
        "type": "metadata_field_related_asset",
        "is_contextable": true,
        "default_value": false,
        "use_default": true
      },
      "Select": {
        "value": "a",
        "fieldid": "4025",
        "type": "metadata_field_select",
        "is_contextable": true,
        "default_value": false,
        "use_default": true
      }
    }
  },
  {
    "asset_name": "OtherChild",
    "asset_assetid": "4034",
    "asset_url": "http://matrix.local/forum13132/listfolder/otherchild",
    "asset_published_short": "1970-01-01",
    "asset_data_metadata": {
      "Related": {
        "value": "4021",
        "fieldid": "4024",
        "type": "metadata_field_related_asset",
        "is_contextable": true,
        "default_value": false,
        "use_default": true
      },
      "Select": {
        "value": "b",
        "fieldid": "4025",
        "type": "metadata_field_select",
        "is_contextable": true,
        "default_value": false,
        "use_default": true
      }
    }
  }
]

---Cut down---

[
  {
    "asset_name": "Test",
    "asset_assetid": "4029",
    "asset_url": "http://matrix.local/forum13132/listfolder/test",
    "asset_published_short": "1970-01-01",
    "asset_data_metadata": {
      "Related": "4022",
      "Select": "a"
    }
  },
  {
    "asset_name": "OtherChild",
    "asset_assetid": "4034",
    "asset_url": "http://matrix.local/forum13132/listfolder/otherchild",
    "asset_published_short": "1970-01-01",
    "asset_data_metadata": {
      "Related": "4021",
      "Select": "b"
    }
  }
]

#3

Thanks JohnGill. Your response was very helpful. Sorry about the late reply. I wanted to make sure I had everything working as intended before providing a response. Few small tweaks at my end, and good to go.

Appreciated!


(Tbaatar) #4

Is it possible to print the schema name that is applied to the asset?

I have 3 schema applied to an asset, and looking to index only the metadata associated with 1 of the schema.

Thanks.