The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.
As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) person.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
For reference, see "Example 1" on the json-simple decoding example page.
Answer from Greg Kopff on Stack OverflowThe whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.
As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...
JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));
for (Object o : a)
{
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
JSONArray cars = (JSONArray) person.get("cars");
for (Object c : cars)
{
System.out.println(c+"");
}
}
For reference, see "Example 1" on the json-simple decoding example page.
You can use jackson library and simply use these 3 lines to convert your json file to Java Object.
ObjectMapper mapper = new ObjectMapper();
InputStream is = Test.class.getResourceAsStream("/test.json");
testObj = mapper.readValue(is, Test.class);
Parse json file in Java?
Read a JSON file with Java "org.json"
streaming a large json object to a file to avoid memory allocation error
Well, JSON.stringify and JSON.parse are synchronous methods that unfortunately you can't pipe to and from. Like u/nissen2 suggests, you'll find some modules out there that will provide you what you're looking for.
Ah, I think I got it. Is there a way to permanently increase the amount of memory that node has access to? Thx.
While this may solve your problem in the mean time, I wouldn't suggest simply allocating more memory (especially with a huge JSON object.)
More on reddit.comHow to edit json files
Videos
I need to parse a JSON file and I don't know how to do so in VSC, I've looked at youtube tutorials, ive looked online and they've all lead me to nothing.
IF im not explaning this right then this is what I'm trying to accomplish (This is in JavaScript)
JSON.parse(fs.readFileSync(INPUT));