This repository has been archived by the owner. It is now read-only.

querystring.stringify can't deal with nested objects #1665

Closed
Hamms opened this Issue Sep 7, 2011 · 12 comments

Comments

Projects
None yet

Hamms commented Sep 7, 2011

require('querystring').stringify({foo : {bar : 'baz'}})

Should return

`````` 'foo[bar]=baz'```

But instead returns
'foo='

isaacs commented Sep 8, 2011

It used to do this. It was terrible.

Use npm install qs, or use foo[bar] as the key in your object.

@isaacs isaacs closed this Sep 8, 2011

AD7six commented Sep 12, 2011

If that behaviour is deliberate and desired it would be useful if at least a warning was issued. Otherwise unless you parse and check the result you don't know that a (potentially big) chuck of info just went missing.

isaacs commented Sep 12, 2011

I agree. Wanna try a patch and send a pull request? Should be about one line. Make sure to sign the CLA: http://nodejs.org/cla.html

AD7six commented Sep 13, 2011

sure thing. #1691 created I hope that's an appropriate change.

I'm curious—why was it terrible?

josser commented Aug 23, 2012

I'm curious too, why this was changed?

Here the use-case:
I want to make a post on the wall via facebook graph api:

Post data:
{message: "something on the wall", privacy:{ value: "CUSTOM", friends: "SELF"}}

So how I can stringify this data and pass result to request module?

kaore commented Aug 3, 2013

Why was it terrible?

Why hasn't this been resolved?

What's going on with this issue?

mrdulin commented Jun 17, 2016

const querystring = require('querystring');

const nestedObj = {
    foo: {
        bar: {
            baz: 'fk'
        }
    }
};

const otherNestedObj = {
    foo: {
        bar: 'fk'
    }
}

const simpleObj = {
    foo: 'bar'
}


console.log('nested object: ' + querystring.stringify(nestedObj));
console.log('other nested object: ' + querystring.stringify(otherNestedObj));
console.log('simple object: ' + querystring.stringify(simpleObj));

result:

dulindeiMac:querystring dulin$ node -v
v6.2.0
dulindeiMac:querystring dulin$ node index.js 
nested object: foo=
other nested object: foo=
simple object: foo=bar

Has this issue been fixed, or is it still necessary to use npm install qs?

Member

TimothyGu commented Jul 20, 2017

@lilyszhang No. If you want to use the nested syntax, use qs.

Why was it terrible?

  • No standard specifying the behavior of nested objects, and corner cases are much more abundant than one might imagine.
  • Space and time complexity. Handling query[a][b][c][][d][3][weird]=2 is slow and complex no matter how you turn it.
  • Node.js core tries to be unopinionated, leaving higher level abstractions to the npm ecosystem.

BTW, it was removed in 422d3c9.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.