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

135
Vistas
How do I change how a POST request is saved in my db.json?

I've created a fake JSON server using npm i json-server in my React application. I've created a db.json file and have created the following function to push a new 'task' to the server for persistence:

const addTask = async task => {
  const res = await fetch(`http://localhost:5000/tasks`, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json'
    },
    body: JSON.stringify(task)
  })

  const data = await res.json()

  setTasks([...tasks, data])
}

Now, this is how my db.json is being saved:

{
  "tasks": [
    {
      "id": 1,
      "text": "Doctors Appointment",
      "day": "Feb 5th at 2:30PM",
      "reminder": true
    },
    {
      "text": "SWE Interview",
      "day": "May 9th at 9:30AM",
      "reminder": false,
      "id": 2
    },
    {
      "text": "School Meeting",
      "day": "Feb 11th at 5:30PM",
      "reminder": false,
      "id": 3
    }
  ]
}

id:1 has been manually entered and id:2 and id:3 have been POST'd in using the UI. How can I make it so that they follow the same pattern of arrangement as id:1 i.e. id, text, day then reminder?

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

0

Order of keys in JSON should not have any affect on the data. If you still need the object to be in a specific order, I would write custom function to stringify the JSON. Here's the code I came up with:

function stringifyTask(task) {
    return `{
    "id": ${task.id},
    "text": "${task.text}",
    "day": "${task.day}",
    "reminder": ${task.reminder}
}`
}
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