-1

Basically, I have a really large JSON file I need to parse, and while searching, I came across this answer.

The only problem is I don't know how to format my JSON array into a single object per line. Is there a straightforward Javascript/Ubuntu way to do this? (I've used jq in the past and it's pretty good for minifying json files, for example)

My JSON file looks something like this

[
  {
    "country":"monrovia",
    "street" :"grove street",
    "where" : "home"
  },
  {
   "country": "uk", 
   "street": "diagon alley", 
   "where": "mystery"
  },
  {
   ...
  }
]

But I need it to look like this

[{"country":"monrovia", "street": "grove street", "where": "home"  },
  {"country": "uk", "street": "diagon alley", "where": "mystery happens"},
  {...}]
King Bee
  • 71
  • 1
  • 14

2 Answers2

0

What you can do is parse the json array by using the JSON.stringify Method like so

// This can be the array of json
var obj = {
    "name": "John Doe",
    "age": 29,
    "location": "Denver Colorado",
};
// stringify the json
var result = JSON.stringify(obj);
// see the output
console.log(result);
Mazhar Khan
  • 302
  • 1
  • 12
0

jq to the rescue once again! Here is what I needed.

And it's apparently referred to as JSONL.

An even better option is 'new-line delimited JSON' (ndjson). The Javascript implementation of the same (with streams!) is here

King Bee
  • 71
  • 1
  • 14