ljharb / qs

A querystring parser with nesting support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing the stringify result would lead to a different object

exneval opened this issue · comments

I don't know if this issue kind of duplicate with #471
So I've tried to stringify an object

const obj = {
  $lookup: {
    '*': [
      {
        products: {
          $lookup: {
            '*': ['categories'],
          },
        },
      },
    ],
  },
};

const queryString = qs.stringify(obj, { encodeValuesOnly: true });

console.log(queryString); // $lookup[*][0][products][$lookup][*][0]=categories

Then, using the stringify result to parse back to object again

const queryObject = qs.parse(queryString);

console.log(JSON.stringify(queryObject, null, 2));
// {
//   "$lookup": {
//     "*": [
//       {
//         "products": {
//           "$lookup": {
//             "*": {
//               "[0]": "categories"
//             }
//           }
//         }
//       }
//     ]
//   }
// }

as we can see, the parsed result is not the same as original object
Please kindly check it @ljharb

The issue is the depth - qs.parse(queryString, { depth: 50 }); produces the expected object, for example. The default of depth in the docs is 5.