SSJS JS parse JSON data

627 views Asked by At

I am trying to retrieve and parse data from API. I this case I get from API request Email details and I need to put to variable some content. The issue is that key is randomly generated but always is the same place.

var mainContent = Platform.Function.ParseJSON(response)['views']['html']['slots']['main']['blocks']['4b1qpkijj8x']['content']

Unfortunately puting [0] in place of '4b1qpkijj8x' doesn't work.

var mainContent = Platform.Function.ParseJSON(response)['views']['html']['slots']['main']['blocks'][0]['content']

How can I manage it?

API Response

1

There are 1 answers

0
Kureteiyu On

Maybe you could use Object.keys() like

let res = Platform.Function.ParseJSON(response)
let key = Object.keys(res['views']['html']['slots']['main']['blocks'])[0]
let mainContent = res['views']['html']['slots']['main']['blocks'][key]['content']