Search code examples
jsongetjson

Why is this an invalid json?


Why is this an invalid JSON, I'm also tried putting quotes to the key and doesn't work.

{
Nombre: “Manzana”,
Imagen: “./img/imagen_manzana.jpg”,
Descripción: “Manzana rica”,
PrecioKG: 24,
Iva: 16,
Inventario:30 }, {
Nombre: “Naranja”,
Imagen: “./img/imagen_naranja.jpg”,
Descripción: “Naranja rica”,
PrecioKG: 14,
Iva: 16,
Inventario: 27 }


Solution

  • For several reasons:

    1. The keys are not in double quotes.

    2. The strings are quoted with "fancy quotes" (), not correct quotes ".

    3. You have two top-level values. There can only be one top-level value. If you want two objects, you need to wrap them in an array.

    http://jsonlint.com (no affiliation) and other such sites can help you with these things.

    Minimal fix:

    [
        {
    
          "Nombre": "Manzana",
    
          "Imagen": "./img/imagen_manzana.jpg",
    
          "Descripción": "Manzana rica",
    
          "PrecioKG": 24,
    
          "Iva": 16,
    
          "Inventario": 30
      },
      {
    
          "Nombre": "Naranja",
    
          "Imagen": "./img/imagen_naranja.jpg",
    
          "Descripción": "Naranja rica",
    
          "PrecioKG": 14,
    
          "Iva": 16,
    
          "Inventario": 27
      }
    ]