How to split long json array line to more lines in source code


#1

I try to use json data in array. its a long list. as long i have all json data on one line its fine.
Below code is example and not the rwal code iim writing.

var pets = '{"pets":[{"name":"jack", "age":"12"},{"name":"john", "age":"14"},{"name":"joe", "age":"18"}]}';

var arr = JSON.parse(pets);

print(arr.pets[1].name);
print(arr.pets[1].age);

But i have so many json lines that i like to split it in the source code on more lines, it mkaes it more readable to.

var pets = '{"pets":[
    {"name":"jack", "age":"12"},
    {"name":"john", "age":"14"},
    {"name":"joe", "age":"18"}
    ]}';    

var arr = JSON.parse(pets);

print(arr.pets[1].name);
print(arr.pets[1].age);

But, i get punished with some error.

Uncaught exception at ( “h:/Scripts/SMRR-HiFi/Json array test-1.js” ) line 2 : “SyntaxError: Parse error”

So how can i use a very long json line and keep it readable without errors ?


#2

The script is complaining because you can’t continue strings over end-of-lines.

But you don’t actually need JSON to create an array of objects like this. Instead, and simpler …

var pets = [
	{"name":"jack", "age":"12"},
	{"name":"john", "age":"14"},
	{"name":"joe", "age":"18"}
];

print(pets[1].name);
print(pets[1].age);

#3

Big thanks :smile: That are the little differences in programming. and some things i never used. now i can move on.