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

1.6K
Vistas
Expected a JSON object, array, or literal

I've copied code from a course I'm taking and it's throwing an error. The message reads "Expected a JSON object, array, or literal" and I'm not entirely sure what I've done wrong since I'm new to using API's (fetch). Here is the screenshot from VSCode. https://www.screencast.com/t/D5oreexoCKq.

In addition to that, this is the error that I'm getting in Chrome. https://www.screencast.com/t/Je4crxQQ4sz6

file -----> data.json

const json = {
    "books": [
        {
            "title": "Learn to Code",
            "author": "John Smith",
            "isbn": "324-23243"
        }, {
            "title": "The Adventures JSON",
            "author": "Jason Jones",
            "isbn": "3324-2-444"
        }, {
            "title": "New Objects",
            "author": "Jane Doe",
            "isbn": "2343-234-2433"
        }
    ]
}

file -----> app.js

JSON.books.forEach(function(val){
  console.log(val);
})
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In a json file , there is no need to declare a data with const keyword.

your data.json should be like this

{
  "books": [{
    "title": "Learn to Code",
    "author": "John Smith",
    "isbn": "324-23243"
  }, {
    "title": "The Adventures JSON",
    "author": "Jason Jones",
    "isbn": "3324-2-444"
  }, {
    "title": "New Objects",
    "author": "Jane Doe",
    "isbn": "2343-234-2433"
  }]
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your script in app.js needs to actually fetch the JSON file. Using the JSON object in the way you have done will not work. JSON.books is undefined.

Do something like

fetch("books.json").then(r=>r.json()).then(({books})=>{
  books.forEach(book=>console.log(book))
})

in your app.js file. This is assuming that book.json can be found in the same folder as app.js and index.html.

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