Home > other >  How to structure json object for javascript project
How to structure json object for javascript project

Time:11-30

I am creating a data structure for my project. It uses json. I am trying to make it easy to search or EDIT the json object. Which structure would be standard. Is there a right or wrong why for would it be okay for me to use either

object with id 2 has parent which is id 1

const items = [
    {
        id: 1,
        title: 'First',
        UUID: 123,
        action : null,
        isFolder: true,
        parent: null,
    },
    {
        id: 2,
        title: 'Second',
        UUID: 123,
        action : null,
        isFolder: false,
        parent: 1,
    },
    {
        id: 3,
        title: 'Third',
        UUID: 123,
        action : null,
        isFolder: false
        parent: null,
    },
]

structure 2

const items = [
    {
        id: 1,
        title: 'First',
        UUID: 123,
        action : null,
        isFolder: true,
        children: [      
            {
                id: 2,
                title: 'Second',
                UUID: 123,
                action : null,
                isFolder: false,
                children: [],
            },
                ]
    },
    {
        id: 3,
        title: 'Third',
        UUID: 123,
        action : null,
        isFolder: false
        children: [],
    },
]

CodePudding user response:

I think your question will be flagged because it's going to get opinion based answers, but here goes: option 1. Why? It's the simplest structure, it will be easy to edit individual nodes, to search, to be read by a human (imagine a model where your children have children have children and it gets hard to look at). Also, you should consider what has to happen if you want to change a parent? What if in the future you need to support multiple parents?

At the end of the day you could probably make arguments for either, so you need to choose what you think fits your application, use case, etc. Do you have any idea of what future enhancements might look at? Consider those.

  • Related