An open API service indexing awesome lists of open source software.

https://github.com/formspark/formson

FormData to JSON converter
https://github.com/formspark/formson

Last synced: 4 months ago
JSON representation

FormData to JSON converter

Awesome Lists containing this project

README

          

# Formson

Formson is a FormData to JSON converter.

It supports:

- Dot notation to create nestable objects
- Square bracket notation to create arrays
- Mixed object/array structures

Sponsored by Formspark, the simple & powerful form solution for developers.

## Installation

Add the Formson script.

```html

```

You can now use the `Formson.toJSON` function to convert a FormData object to a JSON object.

```javascript
const json = Formson.toJSON(formData);
```

## Examples

### Basic example

```html





Example





Submit


document
.getElementById("form")
.addEventListener("submit", function (event) {
event.preventDefault();
const formData = new FormData(this);
const json = Formson.toJSON(formData);
alert(JSON.stringify(json));
});

```

### Dot notation

Dot notation uses periods (`.`) to create nested objects.

```html


```

This will result in the following JSON structure:

```json
{
"user": {
"firstName": "John",
"lastName": "Doe"
}
}
```

### Square bracket notation

Square bracket notation uses square brackets (`[]`) to create arrays.

```html



```

This will result in the following JSON structure:

```json
{
"users": ["Alice", "Bob", "Charlie"]
}
```

### Complex structure

You can mix dot and square bracket notation to create complex structures.

```html












Submit

```

This will result in the following JSON structure:

```json
{
"user": {
"name": "John Doe",
"skills": ["JavaScript", "TypeScript"],
"address": {
"street": "123 Main St",
"city": "Anytown"
},
"projects": [
{
"name": "Project A",
"tasks": ["Task 1", "Task 2"]
},
{
"name": "Project B",
"tasks": ["Task 3"]
}
]
}
}
```

## License

[MIT](https://opensource.org/licenses/MIT)