String with linebreaks to JSON

I have an email that I want to parse and make a JSON of. The reason for this is to add the incomming emails to and database with the information.

I’m getting the plain email text and want to have this as JSON. This is content as example:

"ufeff rnrnrnrnPostcode: rnrnrn rnrn1234aa rnrnrnPlaats: rnrnrn rnrnAmsterdam rnrnrnStraatnaam: rnrnrn rnrnpiet van egmondstraat rnrnrnHuisnummer: rnrnrn rnrn12d rnrnrnType opdracht: rnrnrn rnrnFiets reparatie rnrnrnGewenste bekleding: rnrnrn rnrnPVC / Vinyl rnrnrnAantal treden: rnrnrn rnrn13 rnrnrnModel fiets: rnrnrn rnrnfiets met auto (test) rnrnrnProjectbeschrijving: (optioneel) rnrnrn rnrnLorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia rnrnrnAanhef: rnrnrn rnrnDhr. rnrnrnAchternaam: rnrnrn rnrntest van test rnrnrnE-mailadres: rnrnrn [email protected] <mailto:[email protected]>  rnrnrnTelefoonnummer: rnrnrn rnrn0123456789 rnrn rnrn"

and i would like a result like this:

{Postcode: "1234aa", Plaats: "Amsterdam", Straatnaam: test van teststraat", Huisnummer: "12d", Type opdracht: "Fiets reparatie", Gewenste bekleding: "PVC", Aantal treden: "13", Model fiets: "fiets met auto (test)",  Projectbeschrijving (optioneel): "Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia", Aanhef: "Dhr.", Achternaam: "test van test", E-mailadres: "[email protected]", Telefoonnummer: "0123456789"}

the code that i have at the moment is:

let obj = require("./mail.json");

var str = obj.plain;

someText = str
  .replace(/(rn|n|r)/gm, "", "")
  .slice(2)
  .replace(/s+/g, " ")
  .trim();

console.log(someText);

Is this even possible with Nodejs and Expressjs? The keys (eg. Plaats) are always the same, values may change.

I’ve also managed to remove the line breaks and get a string of all. But after that i’m getting stuck.

I also have the html from the email, would be easier to use the html in the email? Or is this the best way to do it?

Feel free to ask anything or tell a better way if you know it.

Answer

You should be able to do this with String.split and Array.reduce.

We start by turning the string into an array using String.split, then we use Array.filter to eliminate empty entries.

We then turn the array into a map by using each pair of array entries to as key, value pairs.

const s = "rnrnrnrnPostcode: rnrnrn rnrn1234aa rnrnrnPlaats: rnrnrn rnrnAmsterdam rnrnrnStraatnaam: rnrnrn rnrnpiet van egmondstraat rnrnrnHuisnummer: rnrnrn rnrn12d rnrnrnType opdracht: rnrnrn rnrnFiets reparatie rnrnrnGewenste bekleding: rnrnrn rnrnPVC / Vinyl rnrnrnAantal treden: rnrnrn rnrn13 rnrnrnModel fiets: rnrnrn rnrnfiets met auto (test) rnrnrnProjectbeschrijving: (optioneel) rnrnrn rnrnLorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia rnrnrnAanhef: rnrnrn rnrnDhr. rnrnrnAchternaam: rnrnrn rnrntest van test rnrnrnE-mailadres: rnrnrn [email protected] <mailto:[email protected]>  rnrnrnTelefoonnummer: rnrnrn rnrn0123456789 rnrn rnrn";

const result = s.split(/[rn:]+/).map(x => x.trim()).filter(x => x).reduce((acc, curr, index, arr) => { 
    if (index % 2 === 1) {
        acc[arr[index-1]] = curr; 
    }
    return acc;
}, {});
console.log("Result:", result)