0

I have json like below in our legacy system. In general, my json is very large. To make things simpler and easier to understand I have reduced the entries.

{
    "clientSettings": [{
            "clientId": 12345,
            "key": "abc",
            "value": false
        },
        {
            "clientId": 12345,
            "key": "def",
            "value": false
        },
        {
            "clientId": 12345,
            "key": "ghi",
            "value": false
        },
        {
            "clientId": 9876,
            "key": "lkmn",
            "value": false
        }
    ],
    "clientGroupSettings": [{
            "clientDataId": 11,
            "key": "jkl",
            "value": true
        },
        {
            "clientDataId": 11,
            "key": "mno",
            "value": true
        },
        {
            "clientDataId": 12,
            "key": "jkl",
            "value": true
        },
        {
            "clientDataId": 12,
            "key": "mno",
            "value": true
        }
    ],
    "productSettings": [{
            "configurationDataId": 11,
            "key": "jkl",
            "value": true
        },
        {
            "configurationDataId": 11,
            "key": "mno",
            "value": true
        },
        {
            "configurationDataId": 12,
            "key": "jkl",
            "value": true
        },
        {
            "configurationDataId": 12,
            "key": "mno",
            "value": true
        }
    ],
    "customerSettings": [{
            "key": "enableData",
            "value": false
        },
        {
            "key": "minPriceValue",
            "value": "1.0"
        }, {
            "key": "presentData",
            "value": "AEGIS"
        }
    ]
}
  • In clientSettings json array we have clientId's and their keys/values. For single clientId, I can have multiple different keys and values. For example - 12345 clientId has different keys and values as shown below.
  • Similarly it's the same thing for clientGroupSettings as well.
  • And same for productSettings as well.
  • But for customerSettings I just have different keys and values.

I am thinking to redesign the above json so that I don't have to duplicate clientId, clientDataId and configurationDataId for each key and values where they are same. As of now my json is huge because I have lot of ids which are same but with different keys and values.

What's the best way to reorganize this so that I don't have to duplicate clientId, clientDataId and configurationDataId?

I am more interested in re-designing clientSettings, clientGroupSettings and productSettings json array.

3
  • There is no best way. It depends what you need to do with it. What have. you tried?
    – joshp
    Commented Oct 1, 2020 at 23:25
  • I just want to make sure we don't duplicate clientId, clientDataId and configurationDataId for each key and values. I was thinking to make json array for each of those ids?
    – David Todd
    Commented Oct 1, 2020 at 23:36
  • Why do you have {"key": "jkl", "value": true}, {"key": "mno", "value": true}, etc. instead of {"jkl": true, "mno": true}? Commented Oct 2, 2020 at 10:13

1 Answer 1

1

There are lots of ways to organize JSON. Here are two...

1. Array of clientId objects each containing an array of entries

{
  "clientSettings": [
    {
        "clientId": 12345,
        "entries": [
             {"key": "abc", "value": false},
             {"key": "def","value": false},
             {"key": "ghi","value": false}
        ]
    },
    {
        "clientId": 9876,
        "entries": [
             {"key": "lkmn", "value": false}
        ]
    }
  ]
}

2. Object Keyed by clientId, containing object Keyed by your "key" property.

{
    "clientSettings": {
        "12345":
            {
                "abc": false,
                 "def": false,
                 "ghi": false
            },
        "9876": {
                 "lkmn": false
            }
    }
}

Which is better?

It depends on your needs, and to some degree on the tools you use.

  • The second form guarantees that a clientId will not appear twice and a key will not. appear twice for a clientId.
  • The first form documents what the clientId and other fields are.

Ease of use depends a lot on toolset and developer proficiency.

For example, in Javascript and Java they are equally easy to navigate. In Java they are equally easy to deserialize into Collections/Map classes or your own named classes, if you are proficient with a good JSON serializer like Jackson or GSON.

Deciding which is better, or whether some other is better depends on having some criteria. Compactness? Self-Documenting? Enforces cardinality rules? Contains type info for deserializing? Etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.