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

189
Vistas
Getting object key and value from objects in a text file

I'm using fetch to get visemes from amazon polly as a text file and trying to get the value of each object key time and value key but every time I try to it gives me an undefined error and when I tried using a console.log(out[i]); it just gave me the very first quote in the curly brackets. I even tried adding a JSON.parse(out) to my code but It gave me an Uncaught (in promise) SyntaxError: Unexpected token { in JSON at position 39 error. If you need to see my source code I've attached it below. I've also attached links to this question one that shows the contents of my text file and another that shows the error I got before using the JSON.parse. I hope it helps.

var viseme    = 'value';
var time      = 'time';
var bracket   = '}';
var jsonIndex = -1;
var itemHTML  = '';
var number    = '4';
               
fetch(url)
.then(res => res.text())
.then((out) => {
  console.log("Checkout this JSON!", out);
  console.log(out.substring(27, 37));
  var jsonParsed = JSON.parse(out);
  for(var i of Array(12).keys()) {
    console.log(out[1]);
    var anotherResJson = out[i]['@value'];
    console.log("jsonParsed[",i,"][@value]:",anotherResJson);
  }
})
.catch(error => {
  throw error
});

contents of text file

output before JSON.parse

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The contents of your file, from what I see in the screenshot, does not appear to be valid JSON. What I am seeing is several individual objects with double-quotes around the values and keys, but they are separated by newlines. This is not valid JSON and will not parse-- run the below snippet with the console open:

const notActuallyJson = '{ "Batgirl": "Barbara Gordon" }\n{ "Supergirl": "Kara Zor-El" }';
console.log(notActuallyJson);
const parsed = JSON.parse(notActuallyJson);
console.log(parsed);

If you are looking for a data structure similar to this that is valid JSON, you want an array ([]) in which these objects are comma-separated:

const validJson = '[\n{ "Batgirl": "Barbara Gordon" },\n{ "Supergirl": "Kara Zor-El" }\n]';
console.log(validJson);
const parsed = JSON.parse(validJson);
console.log(parsed);

UPDATE

Converting the file contents to JSON

It is hard to know exactly the situation with your file contents because you haven't shared them as text-- you've only shared them as an image. However, if my hunch is correct and they are just valid JSON objects separated with newlines, you could wrap them with brackets and separate them with commas like so:

const notActuallyJson = '{ "Batgirl": "Barbara Gordon" }\n{ "Supergirl": "Kara Zor-El" }';
const convertedToActualJson = `[${notActuallyJson.split('\n').join(',')}]`;
console.log(convertedToActualJson);
const parsed = JSON.parse(convertedToActualJson);
console.log(parsed);

If your file contents does not fit that description, then you'll need a more customized solution, which you should probably post in a separate question as it is diverging quite a bit from the original post.

about 4 years ago · Juan Pablo Isaza Denunciar
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