Stringifying using different arrayFormats, then parsed, create different things
See original GitHub issueIt 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?
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (7 by maintainers)
Top Results From Across the Web
JSON.stringify() - JavaScript - MDN Web Docs
Using the replacer function allows you to control the order of the array elements by returning a different array.
Read more >Output array as comma separated with querySelector
I start with a URL search string and then parse it using qs I then tried the qs stringify method to return the...
Read more >qs - npm
There are 12870 other projects in the npm registry using qs. ... A querystring parsing and stringifying library with some added security.
Read more >query-string - npm.io
query-string. Parse and stringify URL query strings ... The character used to separate array elements when using {arrayFormat: 'separator'} .
Read more >How to use JSON.stringify() and JSON.parse() in JavaScript
parse around JSON.stringify to make a deep copy of an array or object — meaning deeply nested arrays or objects will be copied....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nigeldunne in this case,
%2c
isn’t a comma to be used for splitting. if you parsefoo=a,b
it will split properly.I’d say the issue is that
stringify
needs to produce an unescaped comma when arrayFormat is “comma”.@ljharb The “round trip” still does not work when arrayFormat is “comma”: