0

This is a valid JSON array adopted from an XML string, but I am not sure that if it is structured optimally. It represents an employee schedule with hierarchy of schedule->employees->employee>tasks->task Should tasks be a hierarchy below employee? Am I using [] square brackets in the right places?

{
    "schedule": {
        "employees": [
            {
                "employee": "1000",
                "tasks": [
                    {             
                        "task1": {
                            "site":"McDo",
                            "from":"0900",
                            "to":"1000"

                        }
                    },
                    {                     
                        "task2": {
                            "site":"McDo",
                            "from":"0900",
                            "to":"1000"                            
                        }
                    }
                ]
            },
            {
                "employee": "2000",
                "tasks": [
                    {             
                        "task3": {
                            "site":"HJ",
                            "from":"0900",
                            "to":"1000"

                        }
                    },
                    {                     
                        "task4": {
                            "site":"KFC",
                            "from":"0900",
                            "to":"1000"                            
                        }
                    }
                ]
            }
        ]
    }
}

requirements:

  • schedule->employees (1:N)
  • employees->employees (1:N) - not sure if I need this
  • employee->tasks (1:N)
  • tasks-> task (1:N) - not sure if I need this
  • task->site (1:1)
0

2 Answers 2

1

This really depends on your requirements. Consider this: does a single employee have more tasks, without tasks being shared across multiple employees? In this case you're right.

Otherwise, please share your requirement / usecase with us.

Sign up to request clarification or add additional context in comments.

2 Comments

I added requirements. employee may have multiple tasks, but task is unique to him/her.
Well it seems to fit properly. You could (as suggested by Aya) consider removing the layer employees and tasks. Which I'd recommend in case it still fits your case. Reducing complexity in this case is a good thing.
0

If, by "structured optimally", you mean to minimize the size of the JSON, then it depends if the order of the subitems is significant, and if you ever intend to include any other item types within each object.

Assuming the order is not significant, and that schedules can only contain employees, and employees can only contain tasks, then much of the JSON can be factored out.

For example, the object types can be inferred from their relative depths within the structure, and the lists can be removed to give...

{
    "1000":
    {
        "task1":
        {
            "site":"McDo",
            "from":"0900",
            "to":"1000"
        },
        {                     
        "task2":
        {
            "site":"McDo",
            "from":"0900",
            "to":"1000"                            
        }
    },
    "2000":
    {             
        "task1":
        {
            "site":"HJ",
            "from":"0900",
            "to":"1000"
        },
        "task2":
        {
            "site":"KFC",
            "from":"0900",
            "to":"1000"                            
        }
    }
}

...from which all the original data can be reconstructed.

I've assumed the task names, e.g. "task1" are significant.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.