I get this response from a remote server.
A has 100 products\n Brought: \n ID/234 has brought 8 products\n ID/212 has brought 72 products\n Not Brought\n\n B has 0 products\n Brought\n Not Brought\n
When I execute my node js script on the terminal this response is displayed as a table as shown in the screenshot below.
I want this output as JSON.
Here is my code.
function(err, res) {
product=res.content; //store the response in variable
var prodArr = [];
var obj = product.split('\n'); //split the response at new lines
for(var i= 1; i<obj.length; i=i+1)
{
prodArr.push({
data : obj[i]
});
}
console.log(prodArr);
})
}
And here the JSON I get when I execute the above script.
{ data:'A has 100 products' },
{ data: 'Brought: '},
{ data:'ID/234 has brought 8 products ' },
{ data:'ID/212 has brought 72 products ' },
{ data: 'Not Brought' },
{ data:'B has 0 products' },
{ data: 'Brought: '},
{ data: 'Not Brought '},
But I want JSON which should be something like this as shown below :
{
"data":{
"title": "A has 100 products",
"Brought": {
"1" : "ID/234 has brought 8 products",
"2" : "ID/212 has brought 72 products"
},
"Not Brought" : {
}
}
},
{
"data":{
"title": "B has 0 products",
"Brought": {
},
"Not Brought" : {
}
}
}
How do I do that?

JSON.stringifyto create the JSON for it. Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help.