javascriptreactjsjsonjson-server

ERROR Json-server: SyntaxError: Malformed JSON in file: db.json Unexpected token � in JSON at position


I hope you are doing well. I have a problem while using the json-server package (^0.17.3). When I run the command "json-server --watch db.json", it throws the following error:

hi!

Loading db.json SyntaxError: Malformed JSON in file: db.json Unexpected token � in JSON at position 0 at FileAsync.parse [as deserialize] () at C:\Users\LUIS\AppData\Roaming\npm\node_modules\json-server\node_modules\lowdb\adapters\FileAsync.js:41:35

I have tried reinstalling json-server, but the error persists, and I can't think of anything else. I'm providing my JSON file, which I have tested with JSON Lint and it shows as valid.

{    "Usuario": [
     {
       "idusuario": 1,
       "nombre_usuario": "Nombre Usuario 1",
       "clave": "Clave Usuario 1",
       "username": "Username Usuario 1"
     },
     {
       "idusuario": 2,
       "nombre_usuario": "Nombre Usuario 2",
       "clave": "Clave Usuario 2",
       "username": "Username Usuario 2"
     }    ],    "Lista_compras": [
     {
       "idlista": 1,
       "fecha_lista": "Fecha Lista 1",
       "nombre_lista": "Nombre Lista 1",
       "idusuario": 1
     },
     {
       "idlista": 2,
       "fecha_lista": "Fecha Lista 2",
       "nombre_lista": "Nombre Lista 2",
       "idusuario": 2
     }    ],    "lista_producto": [
     {
       "idlista": 1,
       "idproducto": 1,
       "estado": "Estado Producto 1"
     },
     {
       "idlista": 2,
       "idproducto": 2,
       "estado": "Estado Producto 2"
     }    ],    "Producto": [
     {
       "idproducto": 1,
       "nombre_producto": "Nombre Producto 1",
       "precio": "Precio Producto 1",
       "idproveedor": 1,
       "fecha_creacion": "Fecha Creacion Producto 1"
     },
     {
       "idproducto": 2,
       "nombre_producto": "Nombre Producto 2",
       "precio": "Precio Producto 2",
       "idproveedor": 2,
       "fecha_creacion": "Fecha Creacion Producto 2"
     }    ],    "Proveedor": [
     {
       "idproveedor": 1,
       "nombre_proveedor": "Nombre Proveedor 1",
       "fecha_creacion": "Fecha Creacion Proveedor 1"
     },
     {
       "idproveedor": 2,
       "nombre_proveedor": "Nombre Proveedor 2",
       "fecha_creacion": "Fecha Creacion Proveedor 2"
     }    ]  }

I will use it for a React application with Vite.js. I'm providing this information in case there might be any compatibility issues.

[UPDATE] the error is that I was creating the JSON file with Visual Studio code and the text format was adding invalid characters, I did it again in notepad saving it with UTF-8 encoding and it worked correctly c:


Solution

  • File is correct and the problem is not with JSON Server package. I created brand new project, installed json-server@0.17.3, created file "db.json" with the content you provided and got no error. Here are some things to try: