2
Context: I’m developing an application that consumes multiple Apis. The JSON returned by these Apis have their own structure, where it is often not the most intuitive for the programmer to explore them.
Example: suppose I am using an external API, and that this is JSON returned from any route.
{
"arvore": {
"avo": {
"pai": {
"eu": "pedro"
}
}
}
}
Suppose also that the only information that interests me in that JSON is the key value eu
. This way, every time I consume this resource and get this JSON, I will have to access my information of interest so: obj['arvore']['avo']['pai']['eu']
or so obj.arvore.avo.pai.eu
, etc..
Problem/nuisance: access to this information of interest is very verbose. And this can inhibit a little the speed of the development process in a larger scenario, finally.
I was thinking of creating a kind of wrapper for the returned JSON - of the resources I most consume-. For example, instead of accessing the key through its actual structure (obj['arvore']['avo']['pai']['eu']
), I could just give a obj.eu
and then would have exactly the same result. However, before implementing this, I wanted to raise this discussion here. In addition, I thank you.
Mixin with an object of yours that has quick access methods. Mixin is the key word.
– Oralista de Sistemas
It lacked the language of context. Although the format of your explanation is like python, is that it? If it is enough a class that reads the json and makes available what you want, in the desired format, although I would not waste time with it, if it were not the main goal.
– Sidon
As Mr @Sidon said, it would be good to clarify the language in question (whether it is JS or what). I believe that [Edit] the question to put the desired language tag is enough. I would add that being verbose should probably not be a problem, since any good editor has copy&paste and search/replace if he needs to make a change. Depending on the language, a #define would be enough to simplify typing or centralize the path in a specific place. Could still create some variable by reference, something like
eu = &obj['arvore']['avo']['pai']['eu']
– Bacco
Actually I needed this in Python and Javascript. However, to be more organized, I will direct this question to Javascript.
– Pedro Henrique Camargo