Code Monkey home page Code Monkey logo

x-www-form-urlencoded-to-json's Introduction

x-www-form-urlencoded to JSON

A simple web tool that converts an x-www-form-urlencoded string to JSON

license url



Note

Take a look at https://pferreirafabricio.github.io/x-www-form-urlencoded-to-json/ for a live test

⚙ How it works

The conversion is made in 6 simple steps:

x-wwww-form-urlencoded example: name=Tom&surname=Platz&age=68

  1. Blank characters at the start and at the end of the string are removed
  2. Split the string into an array by the & character
[
  'name=Tom',
  'surname=Platz',
  'age=68'
]
  1. Each string inside the array is split again, now by the = character
[
  ['name', 'Tom'],
  ['surname', 'Platz'],
  ['age', 68]
]
  1. Each array inside the array maps the values to the function decodeURIComponent
['name', 'Tom'] -> [decodeURIComponent('name'), decodeURIComponent('Tom')]
['surname', 'Platz'] -> [decodeURIComponent('surname'), decodeURIComponent('Platz')]
['age', '68'] -> [decodeURIComponent('age'), decodeURIComponent('68')]

We need to do this because some values may be encoded, for example: From=%2B12012537162 where %2B, when decoded by decodeURIComponent, will return the + character

  1. We take the array of array, now adequately sanitized, and call the Object.fromEntries, to transform the array of arrays into an Object
{
  name: 'Tom',
  surname: 'Platz',
  age: 68
}
  1. Finally we call JSON.stringify using the third parameter (space) as 2 to have better visualization of the JSON
{
  "name": "Tom",
  "surname": "Platz",
  "age": "68"
}

The final function will be:

function convertToJson(value) {
  return JSON.stringify(
    Object.fromEntries(
      value
        .trim()
        .split("&")
        .map((s) => s.split("="))
        .map((pair) => pair.map(decodeURIComponent))
    ),
    undefined,
    2
  );
}

✨ Features

  • Converts an x-www-form-urlencoded string to JSON
  • Provides a user-friendly interface to input the string and view the converted JSON
  • Allows users to copy the converted JSON to the clipboard
  • Free and online tool

🏄‍♂️ Quick Start

  1. Clone this repository git clone https://github.com/pferreirafabricio/x-www-form-urlencoded-to-json.git
  2. Enter in the project's folder: cd x-www-form-urlencoded-to-json
  3. Open the index.html in your browser

🧱 This project was built with:

  • HTML
  • CSS
  • JavaScript

📚 References

x-www-form-urlencoded-to-json's People

Contributors

leonardorr avatar pferreirafabricio avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

leonardorr

x-www-form-urlencoded-to-json's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.