question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stringifying using different arrayFormats, then parsed, create different things

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
ljharbcommented, Apr 15, 2020

@nigeldunne in this case, %2c isn’t a comma to be used for splitting. if you parse foo=a,b it will split properly.

I’d say the issue is that stringify needs to produce an unescaped comma when arrayFormat is “comma”.

1reaction
nigeldunnecommented, May 4, 2020

@ljharb The “round trip” still does not work when arrayFormat is “comma”:

qs.stringify({ foo: [ ‘a’, ‘b’ ] }, { arrayFormat: ‘comma’ }) ‘foo=a%2Cb’ qs.parse(‘foo=a%2Cb’, { comma: true }) { foo: ‘a,b’ } //expecting { foo: [ ‘a’, ‘b’ ]

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found