I am having an issue where an API is giving me a long string that contains multiple items that need to be broken out into an object. Here is an example of the string:
"Child 1 First Name: Ali\nChild 1 Gender: Female\nChild 1 Hair Color: Blonde\nChild 1 Hair Style: Wavy\nChild 1 Skin Tone: Tan\nChild 2 First Name: Morgan \nChild 2 Gender: Female\nChild 2 Hair Color: Brown\nChild 2 Hair Style: Ponytail\nChild 2 Skin Tone: Light\nRelationship 1 to 2: Brother\nRelationship 2 to 1: Brother\n"
It is using \n to break everything into multiple lines. The string is one property of a larger object being sent by the API. For some reason when I try and use the .split method on it like (full code below) I need to use JSON.stringify in order to get my function to even begin to work.
Here is my full code:
let rawNoteData = order.customer.note;
let data = JSON.stringify(rawNoteData);
let foo = data.split("\n").reduce(function(obj, str, index) {
let strParts = str.split(":");
obj[strParts[0].replace(/\s+/g, '')] = strParts[1];
return obj;
}, {});
console.log(foo);
This is creating really crazy looking objects that look like this:
{'"Child1FirstName': ' Arabelle\\nChild 1 Gender' }
I think my function is only working for the first instance. I am a little uncertain on how to fix this and clean up all of the crazy quotes etc going on.
My goal is to split the string into an object like so:
mongoExDoc: {
child1FirstName: "Ali",
child1Gender: "Female",
child1HairColor: "Blonde",
child1HairStyle: "Wavy",
child1SkinTone: "Tan",
child2FirstName: "Morgan",
child2Gender: "Female",
child2HairColor: "Brown",
child2HairStyle: "Ponytail",
child2SkinTone: "Light",
relationship1To2: "Brother",
relationship2To1: "Brother"
}
From there I will be combining its properties with another object being inserted into MongoDB.
Any help is GREATLY appreciated!