Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Cleanest way to get JSON out of a JSONP wrapped function return

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!