helloworld1234
helloworld1234

Reputation: 327

Better way to structure JSON data format

I have a question on JSON format to structure my data. For example, I have a JSON data structure format below:

{
 'AAA': {
          '1': {Sample Data 1},
          '2': {Sample Data 2},
          '3': {Sample Data 3},
          },
 'BBB': {
          '1': {Sample Data 1},
          '3': {Sample Data 3},
          '4': {Sample Data 4},
          },
 'CCC': {
          '1': {Sample Data 1},
          '3': {Sample Data 2},
          },
 }

Based on the data above, I can get to know that object 'AAA' contains objects '1','2','3'; where object 'BBB' contains '1','3','4'...etc. My question is, how can I structure the data to know that, for object '1', there is also have object 'AAA', object 'BBB' and object 'CCC'? Instead of preparing second set of data like below, which introduce redundant data and update of 1 set need to update the second one.

{
 '1': {
          'AAA': {Sample Data A},
          'BBB': {Sample Data B},
          'CCC': {Sample Data C},
          },
 '2': {
          'AAA': {Sample Data A},
          },
 '3': {
          'AAA': {Sample Data A},
          'BBB': {Sample Data B},
          'CCC': {Sample Data C),
          },
 '4': {
         'BBB': {Sample Data B},
          },
}

Is there any better method/ technique to structure my data so that can easily get data set from different dimension?

What I think of this way can achieve the same objective I guess (or other way round to include 'keys' in object 1,2,3,4), but not sure is a good way to structure data because still need to manually update the 'keys' if new object introduce such as object 'DDD'. Example:

{
 'AAA': {
          '1': {Sample Data 1, keys: ['AAA', 'BBB', 'CCC']},
          '2': {Sample Data 2, keys: ['AAA','CCC']},
          '3': {Sample Data 3, keys: ['AAA', 'BBB', 'CCC']},
          },
 'BBB': {
          '1': {Sample Data 1, keys: ['AAA', 'BBB', 'CCC']},
          '3': {Sample Data 3, keys: ['AAA', 'BBB', 'CCC']},
          '4': {Sample Data 4, keys: ['BBB']},
          },
 'CCC': {
          '1': {Sample Data 1, keys: ['AAA', 'BBB', 'CCC']},
          '3': {Sample Data 2, keys: ['AAA','CCC']},
          },
 }

Upvotes: 0

Views: 943

Answers (1)

Kaidul
Kaidul

Reputation: 15885

{
 'AAA': {
          '1': {Sample Data 1, keys: ['AAA', 'BBB', 'CCC']},
          '2': {Sample Data 2, keys: ['AAA','CCC']},
          '3': {Sample Data 3, keys: ['AAA', 'BBB', 'CCC']},
          },
 'BBB': {
          '1': {Sample Data 1, keys: ['AAA', 'BBB', 'CCC']},
          '3': {Sample Data 3, keys: ['AAA', 'BBB', 'CCC']},
          '4': {Sample Data 4, keys: ['BBB']},
          },
 'CCC': {
          '1': {Sample Data 1, keys: ['AAA', 'BBB', 'CCC']},
          '3': {Sample Data 2, keys: ['AAA','CCC']},
          },
 }

This is still redundant data over network, only in smaller payload.

I will suggest to stick with the first structure. But instead of object for a set of same type of data, you can use an array of objects which is more appropriate :

{
 'AAA': [
          '1': {Sample Data 1},
          '2': {Sample Data 2},
          '3': {Sample Data 3},
         ],
 'BBB': [
          '1': {Sample Data 1},
          '3': {Sample Data 3},
          '4': {Sample Data 4},
         ],
 'CCC': [
          '1': {Sample Data 1},
          '3': {Sample Data 2},
         ],
 }

And to provide the two-way query (for object '1', there is also have object 'AAA', object 'BBB' and object 'CCC'), you can use multiple set of set data-structure on the other end of network to index the two-way information and support the queries.

For example, in Java it would look like this:

Set<Set<String>> objectMap1 = new HashSet<Set<String>>();
// objectMap1['AAA'] = {'1', '2', '3'}

Set<Set<String>> objectMap2 = new HashSet<Set<String>>();
// objectMap2['1'] = {'AAA', 'BBB', 'CCC'}

Hope this helps!

Upvotes: 1

Related Questions