0

I call some data in text file format from an API (https://en120.grepolis.com/data/players.txt). Each line in this text file is a single player with his information (name, ID, etc.). After every player is a line break. My current problem is I have no good strategy how to get each player separated and also call his specific information to push it into a new player object that will be saved in a json array.

Example:

first line in the text file (id, name, alliance_id, points, rank, towns)

6415107,Aragorn2,972,40297,461,4

should become to

{
  "id": 6415107,
  "name": "Aragorn2",
  "alliance_id": 972,
  "points": 40297,
  "rank": 461
  "towns": 4
}

In theory I could detect the first line break, take all characters from 0 to the position of the line break, remove this substring from the text file and process the data. But this feels really dowdy and I can imagine there is any good method with RegEx and match.

Thanks for your help

phng
  • 344
  • 3
  • 12
  • could this help? https://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript-which-contains-comma-in-data – gugateider Jan 29 '20 at 12:16
  • Does this answer your question? [How can I parse a CSV string with Javascript, which contains comma in data?](https://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript-which-contains-comma-in-data) – Yevhen Horbunkov Jan 29 '20 at 12:18

2 Answers2

1

I would recommend a npm module for this scenario. With it will parse your csv-file to a JSON-Object in nodeJS. Checkout: https://www.npmjs.com/package/csv-parser

With this it will build an array of object and will map the property name to the csv head line.

Example csv adjusted for your use case:

id, name, alliance_id, points, rank, towns
6415107,Aragorn2,972,40297,461,4
Burak Ayyildiz
  • 325
  • 1
  • 6
  • 1
    I like your idea and thanks for your tip that my file is a csv. I didnt even recognize it, ups. But I tested different csv parser and they had all a problem with the file. Finally @Sourabh Somanis answer converted the file correctly. – phng Jan 31 '20 at 09:09
1

You can follow following method hope that will work

var data=`6415107,Aragorn2,972,40340,462,4
6394106,BossLelei,,0,1534,0
5384599,stihls,,175,1514,1
6418914,newcastle,,0,1654,0
6394794,Harpocrates,762,0,1591,0`

lines=data.split("\n")
var result = [];
lines.forEach(x=>{
    let d=x.split(",");
    let o={};
    o["id"]=d[0];
    o["name"]=d[1];
    o["alliance_id"]=d[2];
    o["points"]=d[3];
    o["rank"]=d[4];
    o["towns"]=d[5];
    result.push(o)
})
console.log(result)
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27
  • 1
    Thanks a lot for your conversion snippet. I will thoroughly study it to understand how it works. As against different csv parsers your code works. – phng Jan 31 '20 at 09:11