1

I have a file with data written by a lua script that I am trying to convert into JSON. The data is formatted as follows:

{"ID" : 51, "name" : "John", "relationship" : "Friend", "Lat" : 56.166609, "Long" : 27.157364}
{"ID" : 52, "name" : "Sally", "relationship" : "Friend", "Lat" : 55.895501, "Long" : 26.753631}

I need to convert it to:

[
 {
"time" : 0.00, 
"ID" : 51, 
"name" : "John",
"relationship" : "Friend",
"Lat" : 56.166609, 
"Long" : 27.157364
},
{
 "time" : 0.00, 
 "ID" : 52, 
 "name" : "Sally",
 "relationship" : "Friend",
 "Lat" : 55.895501, 
 "Long" : 26.753631
 }
]

Currently I am trying the code below, which works with a single line of data but not with multiple lines of data. I haven't been able to figure out what additional steps are needed for multilpe lines (the actual data file will be hundreds of lines) :

 const response2 = await fetch('Export.log');
 var data2 = await response2.text();
 var formatted = JSON.parse(data2);
 console.log(formatted);
asked Jun 28, 2020 at 23:07
1
  • you need format json output? Commented Jun 28, 2020 at 23:14

1 Answer 1

1

Looks like you want to take data2 and do the following.

var formatted = JSON.parse('[' + data2.trim().replace(/\n/g, ',') + ']')

So take your data, replace all of the new lines with commas, and then wrap it in an array.

The trim is to take off a trailing new line if there are any.

answered Jun 28, 2020 at 23:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.