0

I want to transform values from form's fields into JSON object, but I want it to be in a specific format. Something like this:

{name: "John", surname: "Smith"}

Is there an easy way to do it? I don't want to build each field manually since there might be a lot of them, and I don't want to do it this way

var data = JSON.stringify($("#newUserForm").serializeArray());

because in this case, JSON will look something like this:

[{"name":"name","value":"John"},{"name":"surname","value":"Smith"}]

Maybe there are some jQuery plugins or other tricks to do it in one line of code?

  • `JSON.stringify(Object.assign({}, ...Array.from(formdata, ([name, value]) => ({[name]: value}))));` – Jaromanda X May 19 '18 at 01:53
  • that serialization format won't allow repeating param names, which the URL format supports. you can accum the serializedArray if you want a more compact representation and don't mind dropping repeats. – dandavis May 19 '18 at 02:18

0 Answers0