8

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?

7
  • Are you able to open the file and read it in? Commented Mar 9, 2017 at 23:59
  • 1
    So you want to take an invalid structure and turn it into an Array of objects. Load the file, split new lines, join with comma, wrap with [ and ], and JSON.parse it. OR fix it so it is valid to start and not have to deal with that. Commented Mar 10, 2017 at 0:01
  • 3
    @epascarello Encapsulating one format in another is very common. Line-delimited JSON streams are extremely common. It's probably the best, most straightforward way to handle a stream of JSON objects. Would you rather have an infinitely sized never ending array and buffer data into memory forever? Would you rather hack the parser or implement a streaming parser? There's nothing wrong with line-delimited JSON as long as you know what you're doing and have a good use case for it. Commented Mar 10, 2017 at 0:06
  • @Brad sounds like there is not a good use case for this if OP needs it in an array. ;) Commented Mar 10, 2017 at 0:08
  • You need some way of parsing this as a javascript object. If you're using a backend like node try using fs if you're using javascript in the browser, you'll have to use some ajax request. Commented Mar 10, 2017 at 0:11

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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);

2 Comments

The one above that uses the map, is actually creating a new array, copying the array outside of the map and then returning that one. It defeats the full purpose of the map, since the map exists because it returns the changed data. For not using the returned data, then one should use a forEach or a for cycle.
About the one that uses the join, its probably more optimized since it parses the json in one go instead of per each element. My post was mainly to warn about the improper use of the map.

Your Answer

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.