Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays of objects #42

Open
RangerMauve opened this issue Feb 23, 2017 · 4 comments
Open

Arrays of objects #42

RangerMauve opened this issue Feb 23, 2017 · 4 comments

Comments

@RangerMauve
Copy link

I have something that looks like this:

<input name="foo[0][bar]" value="a"/>
<input name="foo[0][baz]" value="b"/>
<input name="foo[1][bar]" value="c"/>
<input name="foo[1][baz]" value="d"/>

This is being parsed as

{
  "foo": {
    "0": {"bar": "a", "baz": "b"},
    "1": {"bar": "b", "baz": "c"},
  }
}

Since the keys are numbers, I would have expected it to parse it as an array, as so:

{
  "foo": [
   {"bar": "a", "baz": "b"},
   {"bar": "b", "baz": "c"},
  ]
}

I think this is a reasonable setting to use since numeric keys are already used for arrays of strings.

Would a PR for this change be welcome?

@defunctzombie
Copy link
Owner

Would entertain a PR for this. Also will add that you can post process such objects into arrays in your own code in the event that the default hash behavior is not adequate for your needs. In this case, we had to pick something to support for brackets with a value in them and I think we support array index without additional brackets after (per the readme) but we could make an improvement there.

@RangerMauve
Copy link
Author

Yeah, right now I'm post processing the data since I needed it done quickly, but I'll look into it when I have more time next week and get it in so that I won't have to do additional processing for future use cases.

@IanRr
Copy link

IanRr commented Apr 26, 2017

Currently struggling with this same issue.

Not sure I'm up to the task but maybe? But perhaps @RangerMauve has something brewing. I'd be very pleased.

(that having been said... post-proc working fine afaik)

@RangerMauve
Copy link
Author

@IanRr I've added a post processing hook in my code, and I happen to rely on JSON-Schema so I know which parts of my form needs to be an array of objects.

Sadly I'm a bit too busy to work on this and actually don't need it for my main projects at the moment. From doing a quick review of the code, the change needs to be done around here where you'd need to set the existing key to an array. Might also need to look around here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants