Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda