Way to neatly represent array of objects with 2 values in JSON?

78 views Asked by At

I have data in JSON format like this:

"members": [
            "[email protected]",
            "[email protected]",
            "guest_test.com@##internalservicedomain.test.com##"
        ]

I want to add role so:

admin is administrator
user  is member
guest is guest

What is the best way to store it?

I can do it like this:

"members": [
                {name: "[email protected]", role: "administrator"},
                {name: "[email protected]",  role: "member"},
                {name: "guest_test.com@##internalservicedomain.test.com##", role: "guest"}
            ]

Or like that:

"members": [
                "administrators": ["[email protected]"],
                "users": ["[email protected]"],
                "guests: ["guest_test.com@##internalservicedomain.test.com##"]
            ]

But I don't like those solutions, is there a more neat way to represent it? I would prefer the first option, because then I can iterate more easily through the elements, but the overhead is terrible and the second option looks better, but seems complicated to iterate.

1

There are 1 answers

1
Carlos Alves Jorge On BEST ANSWER

This is more correct option in my opinion

"members": [
                {name: "[email protected]", role: "administrator"},
                {name: "[email protected]",  role: "member"},
                {name: "guest_test.com@##internalservicedomain.test.com##", role: "guest"}
            ]

This way you can not only iterate better but if you add new roles in the future it will be much easier to accomodate.

Ultimately you can put role "A" for administrator, "M" for member and so on to cut some kbs...