qs icon indicating copy to clipboard operation
qs copied to clipboard

Object with "number" keys are stringyfied as arrays

Open richarddd opened this issue 3 years ago • 3 comments

If i run this code i would expect the resulting object to be {params: {"2": "1"}}

qs.parse(qs.stringify({params: {"2": 1}}))

Qs parses it as: {params: ["1"]}

richarddd avatar Apr 22 '21 13:04 richarddd

Since qs.stringify({params: {"2": 1}}, { encodeValuesOnly: true }) produces 'params[2]=1', this seems like an issue with parse.

However, while qs.parse('params[2]=1') produces { params: [ '1' ] }, qs.parse('params[2]=1', { allowSparse: true }) produces { params: [ <2 empty items>, '1' ] }, so i think the thing you're looking for is the allowSparse option?

ljharb avatar Apr 23 '21 04:04 ljharb

Hi, we are having the same issue, however we only observe it on numbers up to 20.

So for example, this will work correctly:

{params: {"21": "1"}}

but not this:

{params: {"20": "1"}}

I don't have the allowSparse option (FYI we are using qs version 6.7.0)

Any ideas? Thanks.

dessskris avatar Apr 27 '21 10:04 dessskris

@dessskris thats because the arrayLimit option defaults to 20.

ljharb avatar Apr 27 '21 13:04 ljharb