I'm working with Adobe Scene7 media server which annoyingly returns an image data object as JSONP, with a comment and a wrapping function.
I'm using .fetch() to make the request and want to get some data out of the response.
Hitting the URL returns the following structure of response:
/*jsonp*/
s7jsonResponse({
"set": { /* image data here */ }},
"aCustomPassedIdentifier"
);
I only care about the object returned by s7jsonResponse() but I'm struggling to work out how to parse this JSONP best.
Currently I am getting the text() from the response when ideally I want json()
fetch(url)
.then((response) => response.text())
And then with the response text I'm having to use str.replace to strip out the necessary comment and wrapping function:
fetch(url)
.then((response) => response.text())
.then((data) => {
data = data
.replace("/*jsonp*/", "")
.replace("s7jsonResponse(", "")
.replace(',"aCustomPassedIdentifier");', "")
})
And then finally I can parse the remaining text as JSON:
fetch(url)
.then((response) => response.text())
.then((data) => {
data = JSON.parse(data
.replace("/*jsonp*/", "")
.replace("s7jsonResponse(", "")
.replace(',"aCustomPassedIdentifier");', "")
)
})
It feels like an incredibly hacky way to get what I need. Is there a better way? Is there an alternative method I can call on response in order to get access to just the JSON without the extra stuff?