Skip to content
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
Copy link

Hamms commented Sep 7, 2011

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

Should return

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

But instead returns
'foo='

@isaacs

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

AD7six commented Sep 13, 2011

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

@thatmarvin

This comment has been minimized.

Copy link

thatmarvin commented Oct 26, 2011

I'm curious—why was it terrible?

@josser

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

kaore commented Aug 3, 2013

Why was it terrible?

@ownagedj

This comment has been minimized.

Copy link

ownagedj commented Oct 21, 2015

Why hasn't this been resolved?

@MichaelArnoldOwens

This comment has been minimized.

Copy link

MichaelArnoldOwens commented Jan 14, 2016

What's going on with this issue?

@mrdulin

This comment has been minimized.

Copy link

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
@lilyszhang

This comment has been minimized.

Copy link

lilyszhang commented Jul 19, 2017

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

@TimothyGu

This comment has been minimized.

Copy link
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.
You can’t perform that action at this time.