Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript JSON Data Structuring Fundamentals Question for a Newbie (Best Practice?)

When I am structuring my JSON data objects at the beginning of the process, I find myself wanting the JSON to have an actual ID it can be indexed from. I come from a simple PHP background ages ago where associative arrays were how record sets were always returned from the database.

Is there an understood best way to do this? I am starting to learn Firebase, which is exclusively just a huge JSON Datafile/Database, and they warn that it is best to just use the flat arrays for larger data sets as opposed to coding our own index values.

So here are two simplified JSON objects that illustrate my point, followed by the way to retrieve some targeted data.
Can anyone give me the best practices method for structuring this data?
And if there is not always a best way to do this, I welcome your actual opinions on the matter as well.

I find myself always leaning toward what I call "the altObject" in the example, but I fear this will cause me regrets the farther my journey takes me. I thought I would ask now up front before I created a terrible headache for myself months from now.

JavaScript:
let regularObject = {
  "otherStuff": { "cool": "stuff" },
  "data": {
    "week": [
      {
        "week": 1,
        "infoArray": [
          {
            "id": 1,
            "cool": "stuff"
          },
            {
            "id": 2,
            "cool": "Other stuff"
          }
        ]
      },
      {
        "week": 2,
        "infoArray": [
          {
            "id": 3,
            "cool": "stuff but different"
          },
            {
            "id": 4,
            "cool": "Other cool stuff but different still"
          }
        ]
      }
    ]
  }
};



let altObject = {
  "otherStuff": { "cool": "stuff" },
  "data": {
    "week": {
      "1": {
        "infoArray": [
          {
            "id": 2222222,
            "cool": "stuff"
          },
          {
            "id": 33333333,
            "cool": "Other stuff"
          }
        ]
      },
      "2": {
        "infoArray": [
          {
            "id": 444444444,
            "cool": "stuff but different"
          },
          {
            "id": 555555555,
            "cool": "Other cool stuff but different still"
          }
        ]
      }
    }
  }
};
let weekVar = 1;
let seemsEasierToUse = altObject.data.week[weekVar].infoArray.map((item) => {
  return item;
});
console.log(seemsEasierToUse);


let seemsHarderToUse = regularObject.data.week.find(week => week.week === weekVar).infoArray.map((item) => {
  return item;
});
console.log(seemsHarderToUse);

Which way is best? Better? Thoughts?
 
When I am structuring my JSON data objects at the beginning of the process, I find myself wanting the JSON to have an actual ID it can be indexed from. I come from a simple PHP background ages ago where associative arrays were how record sets were always returned from the database.

Is there an understood best way to do this? I am starting to learn Firebase, which is exclusively just a huge JSON Datafile/Database, and they warn that it is best to just use the flat arrays for larger data sets as opposed to coding our own index values.

So here are two simplified JSON objects that illustrate my point, followed by the way to retrieve some targeted data.
Can anyone give me the best practices method for structuring this data?
And if there is not always a best way to do this, I welcome your actual opinions on the matter as well.

I find myself always leaning toward what I call "the altObject" in the example, but I fear this will cause me regrets the farther my journey takes me. I thought I would ask now up front before I created a terrible headache for myself months from now.

JavaScript:
let regularObject = {
  "otherStuff": { "cool": "stuff" },
  "data": {
    "week": [
      {
        "week": 1,
        "infoArray": [
          {
            "id": 1,
            "cool": "stuff"
          },
            {
            "id": 2,
            "cool": "Other stuff"
          }
        ]
      },
      {
        "week": 2,
        "infoArray": [
          {
            "id": 3,
            "cool": "stuff but different"
          },
            {
            "id": 4,
            "cool": "Other cool stuff but different still"
          }
        ]
      }
    ]
  }
};



let altObject = {
  "otherStuff": { "cool": "stuff" },
  "data": {
    "week": {
      "1": {
        "infoArray": [
          {
            "id": 2222222,
            "cool": "stuff"
          },
          {
            "id": 33333333,
            "cool": "Other stuff"
          }
        ]
      },
      "2": {
        "infoArray": [
          {
            "id": 444444444,
            "cool": "stuff but different"
          },
          {
            "id": 555555555,
            "cool": "Other cool stuff but different still"
          }
        ]
      }
    }
  }
};
let weekVar = 1;
let seemsEasierToUse = altObject.data.week[weekVar].infoArray.map((item) => {
  return item;
});
console.log(seemsEasierToUse);


let seemsHarderToUse = regularObject.data.week.find(week => week.week === weekVar).infoArray.map((item) => {
  return item;
});
console.log(seemsHarderToUse);

Which way is best? Better? Thoughts?
Hi there,

From personal experience, I have seen the id being used at the document level, not at the data array level. This makes sense because you would want a document in nosql to have a unique identifier. As far as arrays go, you are better off just using the array index. Why overcomplicate your life, right? 🙂
 
THANKS for such a quick reply!

At the risk of divulging just how deeply novice I am, are you saying the "document" level and meaning this would make sense:

Code:
let exampleObject = {
    "1": {
        "doc": "level"
    },
    "2": {
        "doc": "another level"
    }
};

versus doing a similar thing embedded more deeply like my earlier examples?

So if I find myself "wanting" to index something when I am deeper in the structure, is that possibly a good indicator that particular data maybe should be standing alone as it's own table/document to begin with and somehow joined with the other data surrounding it?
 
THANKS for such a quick reply!

At the risk of divulging just how deeply novice I am, are you saying the "document" level and meaning this would make sense:

Code:
let exampleObject = {
    "1": {
        "doc": "level"
    },
    "2": {
        "doc": "another level"
    }
};

versus doing a similar thing embedded more deeply like my earlier examples?

So if I find myself "wanting" to index something when I am deeper in the structure, is that possibly a good indicator that particular data maybe should be standing alone as it's own table/document to begin with and somehow joined with the other data surrounding it?
Yup something along those lines 🙂. I hear ya when it comes to wanting to index all the things deep within lol, and yes, when you start to go down that path, it is a good practice to take a step back and see if anything could be pulled into its own document, much like how we would separate data into different tables and use primary/foreign keys to reference data in SQL/MySQL databases. I am one of those to advocate for that rather than to just dump everything into one huge file...
 
Yup something along those lines 🙂. I hear ya when it comes to wanting to index all the things deep within lol, and yes, when you start to go down that path, it is a good practice to take a step back and see if anything could be pulled into its own document, much like how we would separate data into different tables and use primary/foreign keys to reference data in SQL/MySQL databases. I am one of those to advocate for that rather than to just dump everything into one huge file...
The only thing I would change from this json object is the following:
JSON:
let exampleObject = {
    "id": 1,
    {
        "doc": "level",
        [
            item,
            item,
            item
        ]
    },
    "id": 2,
    {
        "doc": "another level"
    }
};
 
I think this last reply here...
The only thing I would change from this json object is the following:
JSON:
let exampleObject = {
    "id": 1,
    {
        "doc": "level",
        [
            item,
            item,
            item
        ]
    },
    "id": 2,
    {
        "doc": "another level"
    }
};
basically summarizes exactly what I am asking at the core level.
And it took this illustration to make me realize it.

Do we want to explicitly be naming the indexes or not I think is ultimately what I was asking.

so like {"id": 1}, versus { "1": [object] }

But now I have a follow-up about your format that I quoted above.
What makes id:1 be the id of the first doc?

How would you associate the "doc": level data with the id:1 ? // /assuming the JSON is created dynamically that is and we won't know the ordering, etc.
 
I think this last reply here...

basically summarizes exactly what I am asking at the core level.
And it took this illustration to make me realize it.

Do we want to explicitly be naming the indexes or not I think is ultimately what I was asking.

so like {"id": 1}, versus { "1": [object] }

But now I have a follow-up about your format that I quoted above.
What makes id:1 be the id of the first doc?

How would you associate the "doc": level data with the id:1 ? // /assuming the JSON is created dynamically that is and we won't know the ordering, etc.

Best practice would be to name the indexes, yes. Regarding your follow-up question 1: When it comes to nosql databases like mongodb, you don't necessarily need an id for each object, especially if they are all going into one file. Adding an id makes things easier when you are searching for a specific object, rather than using any other filtering. As far as the number is concerned, you don't necessarily need to start at 1. You can start at any arbitrary number that would make logical sense to, such as "id" : 1000, as long as you have some kind of sequence afterwards. To answer your last question, I guess it all depends on what you defined "doc" to represent... is it some kind of numerical identifier, such as Department of Corrections: 13579? Does it represent some kind of clearance level?
 
For my last question what I mean is this: What prevents the dynamic data returned from possibly being this below, versus what you typed out?

Code:
let exampleObject = {
    "id": 1,
    "id": 2,
    { "doc": "level", [array]},
    { "doc": "another_level", [array]},
};

As opposed to nesting the id property inside the same object as the "doc" property? Also when I paste yours into a site like jsonformatter.org it errors out. So I am just mildly confused. THANKS! Apologies for the novice questions.
 
For my last question what I mean is this: What prevents the dynamic data returned from possibly being this below, versus what you typed out?

Code:
let exampleObject = {
    "id": 1,
    "id": 2,
    { "doc": "level", [array]},
    { "doc": "another_level", [array]},
};

As opposed to nesting the id property inside the same object as the "doc" property? Also when I paste yours into a site like jsonformatter.org it errors out. So I am just mildly confused. THANKS! Apologies for the novice questions.
I think your code there errors out because of the
Code:
let exampleObject =
Also because with json, you basically need to name everything, except for the root of the document.

if you put the rest in to jsonformatter, you should be fine
JSON:
 {
    "id": 1,
    "data1": {
        "doc": "level",
        "dataArr": ["a","b","c"]
    }
}

or, if "doc" has multiple values, you could do this
JSON:
 {
    "id": 1,
    "data1": {
        "doc": ["a","b","c"]
    }
}

1698779120828.png
 
The only thing I would change from this json object is the following:
JSON:
let exampleObject = {
    "id": 1,
    {
        "doc": "level",
        [
            item,
            item,
            item
        ]
    },
    "id": 2,
    {
        "doc": "another level"
    }
};
I did not paste in the let exampleObject = portion of the text above. I am seeing now how you added "data1": and "dataArr": though!

So I am assuming if there were more than one item with an id, for example "id": 1 is yours, if there was also an "id":2, then the overall JSON would start as an Array right?

Code:
[
    {
        "id": 1,
        "data1": {
            "doc": ["a","b","c"]
        }
    },
    {
        "id": 2,
        "data1": {
            "doc": ["a","b","c"]
        }
    }
]

And circling all the way back to my original question, what I just put above as the ARRAY is "better" than structuring my returned data from the sserver like this:
Code:
{
    "1": {
        "doc": ["a","b","c"]
    },
    "2": {
        "doc": ["a","b","c"]
    }
}
without explicitly naming the "id" field. Using the "better" way, I would then use Array methods to parse through the array and map/find/filter to the "id" property/field of each item to reference them, versus just referencing them by the key value, correct?
 
I did not paste in the let exampleObject = portion of the text above. I am seeing now how you added "data1": and "dataArr": though!

So I am assuming if there were more than one item with an id, for example "id": 1 is yours, if there was also an "id":2, then the overall JSON would start as an Array right?

Code:
[
    {
        "id": 1,
        "data1": {
            "doc": ["a","b","c"]
        }
    },
    {
        "id": 2,
        "data1": {
            "doc": ["a","b","c"]
        }
    }
]

And circling all the way back to my original question, what I just put above as the ARRAY is "better" than structuring my returned data from the sserver like this:
Code:
{
    "1": {
        "doc": ["a","b","c"]
    },
    "2": {
        "doc": ["a","b","c"]
    }
}
without explicitly naming the "id" field. Using the "better" way, I would then use Array methods to parse through the array and map/find/filter to the "id" property/field of each item to reference them, versus just referencing them by the key value, correct?
Typically what the server would return would be an array of objects, yes, and yes, rather than to use keypair, you could also use array methods for search
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom