parse-nested-form-data icon indicating copy to clipboard operation
parse-nested-form-data copied to clipboard

"Duplicate key" error when parsing array-like field with non-numeric key

Open michaelrog opened this issue 2 years ago • 3 comments

Thanks so much for this package!

We're trying to parse FormData from fields that contain associative-array content:

<input type="hidden" name="myField[foo]" value="1">
<input type="hidden" name="myField[bar]" value="2>

Minimal reproduction:

import {parseFormData} from 'parse-nested-form-data'

const formData = new FormData()
formData.append('myField[foo]', '1')
formData.append('myField[bar]', '2')
parseFormData(formData)

Using string keys with array fields like this produces a "Duplicate key" error:

Uncaught Error: Duplicate key at path part array[foo]
    DuplicateKeyError parse-nested-form-data.esm.js:42
    handlePathPart parse-nested-form-data.esm.js:237
    parseFormData parse-nested-form-data.esm.js:363
    parseFormData parse-nested-form-data.esm.js:362

I'm not surprised to see an error here, since JS [] arrays are integer-indexed, but... Given that string-keyed "array" fields are pretty common in forms/apps these days, could parseFormData() be somehow clever enough to parse these entries into a map object instead of a numeric-keyed array?

michaelrog avatar Jul 12 '23 21:07 michaelrog

I can send a fix if the maintainer is okay with this.

jwnx avatar Sep 23 '23 02:09 jwnx

I can send a fix if the maintainer is okay with this.

Hey not the maintainer but could u fork and post it? Would be nice even if the PR doesn't get accepted

WildEgo avatar Sep 24 '23 20:09 WildEgo

any updates on this? I guess the issue is what would the output data structure be? because when its index based the "key" is built into the fact its an array, but where/how do you store the key if its a random string?

JClackett avatar Oct 01 '24 15:10 JClackett