1
\$\begingroup\$

I am making a to-do list application in Python that will show tasks/subtasks/sub-sub-tasks in a tree view, but with higher-level clades of tasks grouped into blocks (e.g., Work block, Home block). So something like:

To Do List
|--Home Tasks
| |--Clean room
| |--Get storage unit
|
|--Work Tasks
 |--Analysis
 |--Sorting
 |--Save Files
 | |--Pull from computer 1
 | |--Walk to computer 2
 | |--Save on computer 2
 |--Sort files
 |--Plot data

My goal is to store the data as JSON, but I am totally new to this format. I am curious about the quality of the following, and any improvements I could make:

{
 "todoList": 
 [
 {
 "taskBlock": "Home", "tasks":
 [
 {"task": "Clean room", "done": false},
 {"task": "Get storage unit", "done": false}
 ]
 },
 {
 "taskBlock": "Work", "tasks":
 [
 {"task": "Analysis", "done": false},
 {"task": "Sorting", "done": false, "subtasks":
 [
 {"task": "Save files", "done": false, "subtasks":
 [
 {"task": "Pull from computer 1", "done": false},
 {"task": "Walk to computer 2", "done": false},
 {"task": "Save on computer 2", "done": false} 
 ] 
 },
 {"task": "Sort files", "done": false},
 {"task": "Plot data", "done": false} 
 ]
 }
 ]
 } 
 ]
}

I am used to XML, and defining a root item, but frankly I wonder if my "root" item here is superfluous (the 'todoList' parent item).

asked Feb 5, 2015 at 1:50
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You are on the right track! Your hunch is correct that JSON doesn't really have a "root" element the way XML does. Actually, it does have a root - the opening {. Read that as "I am starting an object", the way you'd read the opening element in XML. (The thing about JSON that feels weird if you're used to XML is that you don't actually specify what type of object it is. That's the beauty [and danger] of playing fast and loose with types the way JavaScript does.)

Here's a suggested revision:

{
 "id": 100,
 "belongsToUser": "782950ce-f920-4f98-a69e-a899729e6267",
 "created": "2015-02-05T02:40:31Z",
 "lists": [{
 "name": "Home", 
 "tasks": [
 {
 "name": "Clean room", 
 "done": false
 }, {
 "name": "Get storage unit", 
 "done": false
 }
 ]
 }, {
 "name": "Work", 
 "tasks": [
 {
 "name": "Analysis", 
 "done": false
 }, {
 "name": "Sorting", 
 "done": false, 
 "subtasks": [
 {
 "name": "Save files", 
 "done": false, 
 "subtasks": [
 {
 "name": 
 "Pull from computer 1", 
 "done": false
 }, {
 "name": "Walk to computer 2", 
 "done": false
 }, {
 "name": "Save on computer 2", 
 "done": false
 }] 
 },
 {
 "name": "Sort files", 
 "done": false
 }, {
 "name": "Plot data",
 "done": false
 }]
 }]
 }]
}

A few things of note:

  • There's no need for a root todoList attribute, but there's plenty of other good metadata that could go at the root level: primary keys or IDs, timestamps, ownership information, and so on.
  • I think it makes sense to have a lists array at the root, and then have object for each clade (the Home and Work blocks) under that.
  • Each list object can have metadata of its own (name, creation timestamp, etc), in addition to an array of tasks.
neuronet
3851 gold badge6 silver badges14 bronze badges
answered Feb 5, 2015 at 2:45
\$\endgroup\$
2
  • \$\begingroup\$ I should mention - there's no reason why you couldn't specify a type inside a JSON object, and many applications that save to JSON do just that: the MongoDB C# driver uses a "type discriminant" attribute (called _t) when storing serialized objects so it knows what to deserialize them as. The beauty of JSON is that it is very flexible because it is very simple. \$\endgroup\$ Commented Feb 5, 2015 at 2:54
  • \$\begingroup\$ Really helpful suggestions, especially about storing metadata for each item. \$\endgroup\$ Commented Feb 5, 2015 at 15:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.