Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

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

Closed

Description

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

Should return

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

But instead returns
'foo='

Activity

  1. isaacs commented on Sep 8, 2011

    @isaacs

    It used to do this. It was terrible.

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

  2. AD7six commented on Sep 12, 2011

    @AD7six

    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.

  3. isaacs commented on Sep 12, 2011

    @isaacs

    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

  4. AD7six commented on Sep 13, 2011

    @AD7six

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

  5. thatmarvin commented on Oct 26, 2011

    @thatmarvin

    I'm curious—why was it terrible?

  6. josser commented on Aug 23, 2012

    @josser

    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?

  7. kaore commented on Aug 3, 2013

    @kaore

    Why was it terrible?

  8. ownagedj commented on Oct 21, 2015

    @ownagedj

    Why hasn't this been resolved?

  9. MichaelArnoldOwens commented on Jan 14, 2016

    @MichaelArnoldOwens

    What's going on with this issue?

  10. mrdulin commented on Jun 17, 2016

    @mrdulin
    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
  11. lilyszhang commented on Jul 19, 2017

    @lilyszhang

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

  12. TimothyGu commented on Jul 20, 2017

    @TimothyGu
    Member

    @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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions