question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically convert FormData to JSON

See original GitHub issue

Is your feature request related to a problem? Please describe. Since svelte advocates progressive enhancement, I think an utility to automatically convert FormData to JSON can be really helpful to write forms that works w/o JavaScript.

The demo currently does it manually, but once the form becomes complex, the conversion can be cumbersome. e.g.,:

<input name="items[0].name" value="Shoe">
<input name="items[0].price" value="99">
<input name="items[1].name" value="Pants">
<input name="items[1].price" value="99">

Describe the solution you’d like Svelte kit provides an utility that automatically converts FormData to JSON. However, since FormData erases type like number, a mapping between FormData and types is needed. One solution I can think of is that svelte kit allows specifying an attribute like svelte:type="number" on form controls, and svelte kit would parse the types into a manifest keyed by form action, if it points to an endpoint, and the utility is only usable in the endpoint that uses the manifest to generate the corresponding JSON.

Describe alternatives you’ve considered

How important is this feature to you? Not very, since it’s manually convertible.

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Rich-Harriscommented, May 4, 2021

oh wait, I think I misunderstood — you want a mechanism for converting the ReadOnlyFormData inside endpoints to JSON? That seems like something that ought to be done in userland, in the handle function

0reactions
Rich-Harriscommented, Feb 2, 2022

In newer versions, we expose a Request object, and form data is generated with await request.formData(). Anything beyond that belongs in userland, so I’ll close this

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert FormData (HTML5 object) to JSON
var object = {}; formData.forEach((value, key) => object[key] = value); var json = JSON ...
Read more >
How to Convert HTML Form Field Values to a JSON Object
The FormData API doesn't directly convert form values to JSON, but we can get there by using the entries method and passing its...
Read more >
How To Format Form Data as JSON - Section.io
Formating data to JSON and making a POST request ... Passing the FormData instance directly to fetch by default, the request body is...
Read more >
Converting FormData to JSON in JavaScript - Sean C Davis
Converting FormData to JSON in JavaScript. FormData is a useful interface for collecting data from a form. But it can be tricky to...
Read more >
form-data-json-convert - npm
Convert html forms into json or fill forms from json.. Latest version: 2.1.4, last published: 2 days ago. Start using form-data-json-convert ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found