JSON response: cannot read the value of some keys

In a JSON response body. Example:

{
“headers”: {
“date”: [
“Thu, 25 Oct 2018 19:46:37 GMT”
],
“content-type”: [
“application/json”
]

         }

}

When I print the value of “date” key, it prints perfectly but somehow it doesn’t print value for content-type in the test script.

I noticed that it works fine for any key but doesn’t work with a keyname that has - (dash) in it.

does anyone have solution for this?

@jessleen82 Are you trying to print like this console.log(responseJSON.date)?

In javascript, you cannot access properties which have a dash in them using this syntax.
You’ll have to do something like this to access that property:

let sampleResponseJSON = {
    "headers": {
        "date": [
            "Thu, 25 Oct 2018 19:46:37 GMT"
        ],
        "content-type": [
            "application/json"
        ]
    }
}

console.log(sampleResponseJSON.headers['content-type']);
// Prints - ["application/json"]

Hey, thanks for the quick response. yes, that works. Thanks so much! :slight_smile:

1 Like