ljharb/qs

Stringifying using different arrayFormats, then parsed, create different things

Open

#345 opened on Nov 15, 2019

View on GitHub
 (16 comments) (0 reactions) (0 assignees)JavaScript (8,015 stars) (744 forks)batch import
bughelp wantedparsequestionstringify

Description

It seems stringifying my metadata object with arrayFormat: brackets, then parsing it, creates a different object to stringifying with arrayFormat: indices then parsing that string.

> const qs = require('qs');

> metadata = [{key: "a", value: "2"}, {key: "b", value: "3"}]
[ { key: 'a', value: '2' }, { key: 'b', value: '3' } ]

> brackets = qs.stringify({"metadata": metadata}, { arrayFormat: "brackets"})
'metadata%5B%5D%5Bkey%5D=a&metadata%5B%5D%5Bvalue%5D=2&metadata%5B%5D%5Bkey%5D=b&metadata%5B%5D%5Bvalue%5D=3'

> parsed_brackets = qs.parse(brackets)
{ metadata: [ { key: [Array], value: [Array] } ] }

> parsed_brackets["metadata"]
[ { key: [ 'a', 'b' ], value: [ '2', '3' ] } ]

> indices = qs.stringify({"metadata": metadata}, { arrayFormat: "indices"})
'metadata%5B0%5D%5Bkey%5D=a&metadata%5B0%5D%5Bvalue%5D=2&metadata%5B1%5D%5Bkey%5D=b&metadata%5B1%5D%5Bvalue%5D=3'

> parsed_indices = qs.parse(indices)
{ metadata: [ { key: 'a', value: '2' }, { key: 'b', value: '3' } ] }

Am I doing something wrong? Is this expected? Is this just an issue with stringifying/parsing nested objects?

Contributor guide