Tim
Tim

Reputation: 99526

Can a json file contain more than one object?

Can a json file contain more than one object?

For example, can the content of a json file be two objects as the following?

{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"}

Some experienced software engineer said that a json file could only contain one object, although other objects could be nested in the object.

Thanks.

Upvotes: 1

Views: 5768

Answers (2)

Robert Moskal
Robert Moskal

Reputation: 22553

That doesn't look much like json, but yes, you can totally have an array of objects in json file. Something like this in your case:

[{"firstName": "John", "lastName": "Smith"},       
{"firstName": "Jane", "lastName": "Doe"}]

A json file may either contain a single object in (which can be complex, with many nested keys) or an array of such objects. It's either curly braces or square brackets on the outside.

Upvotes: 2

André Senra
André Senra

Reputation: 2235

Yes.

You can have:

JSONObject = { ObjectOne: { property1: value1 }, ObjectTwo: { property1: value2 }, someProperty: value3, .... }

EDIT: You can also have an array of objects if that suits you better.

Upvotes: 0

Related Questions