Skip to content

Instantly share code, notes, and snippets.

@arafathusayn
Created May 31, 2022 12:31
Show Gist options
  • Select an option

  • Save arafathusayn/13009d5bfc054cb1e257eeb298b6bb29 to your computer and use it in GitHub Desktop.

Select an option

Save arafathusayn/13009d5bfc054cb1e257eeb298b6bb29 to your computer and use it in GitHub Desktop.

Revisions

  1. arafathusayn created this gist May 31, 2022.
    15 changes: 15 additions & 0 deletions raw-form-data-to-json.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    function rawFormDataToJSON(formDataStr) {
    return JSON.stringify(formDataStr
    .trim()
    .split('\n')
    .filter(l => !l.startsWith('//'))
    .map(l => l.split(':'))
    .reduce((a, c) => ({ ...a, [c[0]]: c[1] }), {}))
    }

    // USAGE:
    rawFormDataToJSON(`
    year:2022
    user_name:Customer 03
    //id:1
    `)