0

Consider a JSON representation for delivering a package from one address to another. Simplified,

{ "parcelId": 123, "source": { "street": "123 Main Street", "city": "Anytown", "state": "New York" }, "destination": { "street": "456 Avenue B", "city": "Elsewhere", "state": "New Jersey" } } 

I'm fairly sure that keys "street", "city", and "state" can be legally nested in both "source" and "destination" objects. Are there technical reasons that the key names should not be repeated?

1
  • Short: No. What makes you doubt that you can reuse the keys in separate parts of the JSON? Commented Dec 20, 2016 at 15:07

3 Answers 3

1

Are there technical reasons that the key names should not be repeated?

No. Seems perfectly reasonable to me.

e.g. if I was serialising a Scala/Java object, that object could look like:

class Delivery { val parcelId : String val source : Address val destination : Address } 

and the field names of the Address object would be the same here.

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

Comments

1

There is nothing wrong with having duplicate property keys which are part of different objects in JSON.

Your JSON example is perfectly valid.


This is only an issue when they are at the same level.

For example two source objects:

{ "parcelId": 123, "source": { "street": "123 Main Street", "city": "Anytown", "state": "New York" }, "source": { "street": "456 Avenue B", "city": "Elsewhere", "state": "New Jersey" } } 

Or two street properties inside one object:

 "source": { "street": "456 Avenue B", "street": "Elsewhere", "state": "New Jersey" } 

Comments

0

No. This would be confusing if you had delivery.street and then a different delivery.street. But you don't. You have delivery.source.street and delivery.destination.street. Basically the key street is addressing a completely different object now.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.