Ali Lotfi
Ali Lotfi

Reputation: 135

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 \r\n\r\n\r\n\r\nPostcode: \r\n\r\n\r\n \r\n\r\n1234aa \r\n\r\n\r\nPlaats: \r\n\r\n\r\n \r\n\r\nAmsterdam \r\n\r\n\r\nStraatnaam: \r\n\r\n\r\n \r\n\r\npiet van egmondstraat \r\n\r\n\r\nHuisnummer: \r\n\r\n\r\n \r\n\r\n12d \r\n\r\n\r\nType opdracht: \r\n\r\n\r\n \r\n\r\nFiets reparatie \r\n\r\n\r\nGewenste bekleding: \r\n\r\n\r\n \r\n\r\nPVC / Vinyl \r\n\r\n\r\nAantal treden: \r\n\r\n\r\n \r\n\r\n13 \r\n\r\n\r\nModel fiets: \r\n\r\n\r\n \r\n\r\nfiets met auto (test) \r\n\r\n\r\nProjectbeschrijving: (optioneel) \r\n\r\n\r\n \r\n\r\nLorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia \r\n\r\n\r\nAanhef: \r\n\r\n\r\n \r\n\r\nDhr. \r\n\r\n\r\nAchternaam: \r\n\r\n\r\n \r\n\r\ntest van test \r\n\r\n\r\nE-mailadres: \r\n\r\n\r\n \r\n\r\[email protected] <mailto:[email protected]>  \r\n\r\n\r\nTelefoonnummer: \r\n\r\n\r\n \r\n\r\n0123456789 \r\n\r\n \r\n\r\n"

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(/(\r\n|\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.

Upvotes: 0

Views: 161

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30705

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 = "\r\n\r\n\r\n\r\nPostcode: \r\n\r\n\r\n \r\n\r\n1234aa \r\n\r\n\r\nPlaats: \r\n\r\n\r\n \r\n\r\nAmsterdam \r\n\r\n\r\nStraatnaam: \r\n\r\n\r\n \r\n\r\npiet van egmondstraat \r\n\r\n\r\nHuisnummer: \r\n\r\n\r\n \r\n\r\n12d \r\n\r\n\r\nType opdracht: \r\n\r\n\r\n \r\n\r\nFiets reparatie \r\n\r\n\r\nGewenste bekleding: \r\n\r\n\r\n \r\n\r\nPVC / Vinyl \r\n\r\n\r\nAantal treden: \r\n\r\n\r\n \r\n\r\n13 \r\n\r\n\r\nModel fiets: \r\n\r\n\r\n \r\n\r\nfiets met auto (test) \r\n\r\n\r\nProjectbeschrijving: (optioneel) \r\n\r\n\r\n \r\n\r\nLorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia \r\n\r\n\r\nAanhef: \r\n\r\n\r\n \r\n\r\nDhr. \r\n\r\n\r\nAchternaam: \r\n\r\n\r\n \r\n\r\ntest van test \r\n\r\n\r\nE-mailadres: \r\n\r\n\r\n \r\n\r\[email protected] <mailto:[email protected]>  \r\n\r\n\r\nTelefoonnummer: \r\n\r\n\r\n \r\n\r\n0123456789 \r\n\r\n \r\n\r\n";

const result = s.split(/[\r\n:]+/).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)

Upvotes: 1

Related Questions