Skip to content
Advertisement

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
}));

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement