Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda