13

If I want the Node.js query parser to parse an array, I can send this:

'?or=foo&or=bar' // gets me { or: ['foo', 'bar'] }

If I want an object I can do this:

'?or[foo]=bar' // gets me { or: {foo: 'bar'}}

But how do I get an array of objects? I'd like this output:

{ or: [{foo: 'bar'}, {bar: 'baz'}]}
2
  • Convert that to JSON and send it in the URL Encoded format Commented Jan 18, 2015 at 2:55
  • doesn't work. I just get this mess: { '{"or":': { '{"foo":"bar"},{"bar":"baz"}]': '' } } Commented Jan 18, 2015 at 3:00

2 Answers 2

18

With the qs module, you can get the object you're looking for if you use this format:

or[0][foo]=bar&or[1][bar]=baz

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that worked without the module actually. Thanks!
1

You can use options allowDots.

const stringParams = qs.stringify(params, {allowDots:true});
// myArray[0].name=MeWhit
qs.parse(stringParams , {allowDots: true});
// [{ name: MeWhit}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.