1

I am currently working on json api designing for my project. I have searched on internet and find that there are nearly two styles of json. One is like

{
    "user":{
        "id":12,
        "name":"Jack",
        "address":{
            "city":"New York",
            "post":"123456"
        }
    }
}

And the other is like:

{
    "user.id":12,
    "user.name":"Jack",
    "user.address.city":"New York",
    "user.address.post":"123456"
}

I think the first one is OK, but since the second one appears, there must be some reason. So I want to know the differences especially the performance differences between the two formats. And I've searched on google, stackoverflow and some other websites to find an answer. But I haven't got one that is particular. Of course, Why and when do we need to flatten JSON objects? shows some useful information, but still no performance comparison. I'd appreciate it if any one have good answers.

Community
  • 1
  • 1
Bill Grim
  • 11
  • 1

1 Answers1

0

Looks like it's basically a tradeoff between compactness, read performance, and write performance. For example, see this synthetic benchmark on a flat vs nested object 6 nodes deep where access is 33x faster on the flat object: https://jsbench.me/i3laj4m2ch/1. I don't have stats on an "average" increase in size between flat vs nested, but it's going to be considerable on an object of any complexity. Likewise, writes are going to be a LOT slower on a flattened object when modify values at the top of the object tree, since a great many paths will need to be accessed and updated.

You can basically think of the flattened object as a type of key value index on the nested object. And like adding any index to a database, it's going to give you better read performance but at the cost of memory and write performance.

Scottmas
  • 1,082
  • 10
  • 22