javascriptsnowflake-cloud-data-platformsnowsql

How to convert array object in snowflake


I would like to convert the below array of objects into another form (varname is ignored as its not required, key and value is used to generate the output form). Any leads would be appreciated

Input array: [ { "key": "string_U6", "value": "grandwagoneer", "varname": "pagenameplate" }, { "key": "string_U13", "value": "2021", "varname": "year" } ]

Output

[ { "string_U6": "grandwagoneer" }, { "string_U13": "2021" } ]


Solution

  • You could try using map as below:

    var input = [ { "key": "string_U6", "value": "grandwagoneer", "varname": "pagenameplate" }, { "key": "string_U13", "value": "2021", "varname": "year" } ];
    
    var output = input.map(function(entry){
        let obj = {}; 
        obj[entry.key] = entry.value;
        return obj;
    });
    
    console.log(output);