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

123
Visualizações
Prevent node.js from converting a nested object with numeric keys into array

When i have an object with a nested object within, whose keys are numbers and send it to my node.js server, then the nested object is converted to an array. How can i prevent this ?

Client:

$.ajax({
    url : `/cctool/report`,
    method : "PUT",
    data : {
        new : {
            10 : "Test",
            20 : "Hello",
        }
    }
});

Server:

router.put("/cctool/report", (request, response) => {
    console.log(request.body);
}

{ new: [ 'Test', 'Hello' ] }

When i add a not numeric key, everything works fine. Also when the keys are at the first level.

My settings:

const express = require('express');
const CookieParser = require('cookie-parser');
const Sessions = require('express-session');
const crypto = require('crypto');
const router = express.Router();

router.use(express.json());
router.use(express.urlencoded({extended : true}));
router.use(CookieParser());
router.use( Sessions({
    secret : crypto.randomBytes(64).toString('hex'),
    saveUninitialized : false,
    cookie : {maxAge : 1000 * 60 * 60 * 8},     // 8 hours
    resave : false
}));
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try adding the contentType header and using JSON.stringify to serialize the object before passing to $.ajax:

const express = require("express");
const app = express();

const html = `<!DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<script>
$.ajax({
  url: "/cctool/report",
  method: "PUT",
  contentType: "application/json",
  data: JSON.stringify({
    new: {
      10: "Test",
      20: "Hello",
    }
  }),
  success: data => console.log(data),
});
</script></body></html>`;

app
  .use(express.json())
  .get("/", (req, res) => res.send(html))
  .put("/cctool/report", (req, res) => {
    console.log(req.body);
    res.json(req.body);
  })
  .listen(3000);

See jQuery posting JSON.

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to use a String value as a key, if you want to use a number you have to convert it to String type. Source: https://tc39.es/ecma262/#sec-object-type

data : {
        new : {
            "10" : "Test",
            "20" : "Hello",
        }
    }
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