Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through newline separated json line by line in JavaScript?

I have a JSON file in my project that looks like this:

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}

I'm not sure how to loop through each line to put it in an array. How can I do this?

like image 535
user1261710 Avatar asked Mar 09 '17 23:03

user1261710


People also ask

How do you escape a new line in JSON?

JSON strings do not allow real newlines in its data; it can only have escaped newlines. Snowflake allows escaping the newline character by the use of an additional backslash character.

Does JSON Stringify call toJSON?

JSON.stringify() calls toJSON with one parameter, the key , which has the same semantic as the key parameter of the replacer function: if this object is a property value, the property name. if it is in an array, the index in the array, as a string.

What is JSON () in JavaScript?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

Does JSON parse sanitize?

Data from an untrusted source is not sanitized by the server and written directly to a JSON stream. This is referred to as server-side JSON injection. Data from an untrusted source is not sanitized and parsed directly using the JavaScript eval function. This is referred to as client-side JSON injection.


3 Answers

Split on new lines, join with comma, wrap it with brackets, and parse it.

var str = `{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}`

var lines = str.split(/\n/);
var wrapped = "[" + lines.join(",") + "]";
var obj = JSON.parse(wrapped);

console.log(obj);

Better solution, fix whatever gives you that format to give you the correct structure to start out with.

like image 72
epascarello Avatar answered Oct 15 '22 19:10

epascarello


What about using map properly?

var myVar = `{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}`;

var myArray = myVar.split('\n').map(JSON.parse);
console.log(myArray);
like image 20
organom Avatar answered Oct 15 '22 18:10

organom


Try the following code:

var myVar = `{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}`;

var myArray = [];
myVar.split('\n').map(function (value) {
  myArray.push(JSON.parse(value));
});
console.log(myArray);
like image 23
Mamun Avatar answered Oct 15 '22 17:10

Mamun