Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda