Denis Sokolov
Denis Sokolov

Reputation: 85

Complex JSON data structure best practice

I would like to know which of JSON data structure is simpler and more convenient for REST API consumers.

Suppose, we have POST method, which require complex data structure in request body. Which of structures is more preferable? They are equavalent.

1.

{
    SearchPropertiesFilter: [
        { key: 'key1', values: ['value1', 'value2'] },
        { key: 'key1', values: ['value3', 'value4'] },
        { key: 'key2', values: ['value5'] }
    ],
    ResultPropertiesCount: [
        { key: 'key1', count: 100},
        { key: 'key2', count: 500},
    ]
}

2.

{
    SearchPropertiesFilter: {
        'key1': [['value1', 'value2'], ['value3', 'value4']],
        'key2': [['value5']]
    }
    ResultPropertiesCount: {
        'key1': 100,
        'key2': 500
    }
}

On one hand, first example may simpler for consumer. On other hand, second example is shorter and don't contain property names.

Upvotes: 1

Views: 2710

Answers (1)

meda
meda

Reputation: 45500

By definition

JSON is built on two structures:

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

So JSON is already a key-value pair system, what’s the point of the redundancy of having pairs with a type key and a value key when key:val will do?

go for version 2

Upvotes: 3

Related Questions