JSON & Javascript Tutorial?

I’ve created a shortcut that uses Data Jar to ease reading and writing to a JSON store. This is working pretty well, but I think it would be better in Drafts , because the prompts are more flexible (or maybe Scriptable? But I don’t think it has the support for prompts for user input out of the box). However, I am barely a novice with javascript and don’t know much about working with JSON. Everything I can find online is either bare man pages that assume more expertise than I have, or seems to assume some kind of Web / HTML usage that doesn’t apply here, where I just want to read/write values to keys, and also create new objects.

Here’s an example of the data:

{
  "1": {
    "creation date": "2019-12-24",
    "name": "name 1",
    "priority": {
      "completion date": "",
      "creation date": "2019-12-26",
      "daily": [
        {
          "complete": "False",
          "date": "2019-12-28",
          "name": "not done"
        }
      ],
      "enough": [
        {
          "date": "2019-12-27",
          "description": "Something",
          "enough?": "true"
        },
        {
          "date": "2019-12-27",
          "description": "Nothing",
          "enough?": "false"
        }
      ],
      "name": "name 1a"
    }
  },
  "2": {
    "creation date": "2019-12-24",
    "name": "name 2",
    "priority": {
      "completion date": "",
      "creation date": "2019-12-26",
      "daily": [
        {
          "complete": "True",
          "date": "2019-12-28",
          "name": "done"
        }
      ],
      "enough": [
        {
          "date": "2019-12-27",
          "description": "Nothing",
          "enough?": false
        },
        {
          "date": "2019-12-27",
          "description": "Nothing",
          "enough?": "false"
        }
      ],
      "name": "Name 2a"
    }
  },

Basically, on a daily basis, I want to read “name” keys and write new arrays to “enough”. and “daily”

Not looking for someone to take on this ambitious coding challenge, but any resources you think would be useful, I’d love to hear about them.

JSON supports Booleans, so you don’t need to write them as strings.

(that would also fix the inconsistency in your example: “false” vs “False”)

1 Like

Presumably JSON supports any javascript object attributes.

Ok, cool I will fix that.

I am having the hardest time trying to figure out the right search terms for what I want to do. Basically, I want to add new values to the “enough” and “daily” keys. I think what I want to do is add a “dictionary” to an “array”? But this isn’t turning up any useful examples. I can’t tell what kind of code to use. Basically, I’m looping over the top level keys (“1”, “2”) then writing new keys in “enough” or “daily”. I thought something like this might work, but it doesn’t:

for (var goal in json){
    json[goal].enough.unshift({"date":dStamp,"description":field,"enough?":toggle});
}

When running this code in Drafts, I get this error: Type error, undefined is not an object

Try this…

That looks like exactly what I’m trying to do, thanks! However, I need to go in a level, and I think that’s where things are going wrong. The real code is back at the Drafts forum.

The dot notation just occurs at each level and you can use standard array syntax just as you were doing above.

I’ve posted a code revision over on that thread for you to take a look at and see if it does what you are trying to do.

It’s a bit off-topic, but can you explain how you use the “enough?” dictionary in real life?

I signed up for this course on “goals crafting” that’s happening at the end of the week and in going through the introductory materials, I got interested in their web app for managing goals. I think I will probably use it for a few months and see what it’s like, but one of the issues is that it doesn’t connect with my other apps, so I thought I might see if I could build some of it into Drafts so I could send data around to different places as well.

At any rate, the process is something like this:

  1. Identify a goal
  2. Identify the top priority for meeting that goal
  3. On a daily basis, identify what your intentions are for meeting that goal (basically, the tasks for the day)
  4. At the end of the day, reflect on the outcomes–did I do “enough” to move this goal forward today? [This is where I have a boolean checkbox and a place to write a bit. Some days I don’t do anything toward a goal, but it’s still “enough” because of other commitments and it’s nice to describe that.]
  5. Rinse and Repeat.

One of the things that this system promotes is starting fresh every day, rather than storing tasks in a system that gets stale. I know some folks need their robust OmniFocus databases to manage things, but for me, these tasks lists start to get weighed down by stuff I “should” do but no longer want to. This app is the first that seems to throw that on its head and I’m looking forward to getting more deeply into it.

Thank you for explaining. I’m using GTD myself, so I was curious about your “system”.

I’m still not sure I fully comprehend the value of the “enough” field. Is there a moment of reflection (like the Weekly Review in GTD) when you look back on those values and take appropriate action?

Well, I’m finding that doing this every night is the moment of reflection. If I haven’t done enough on the goal that day, when I plan the next day (which happens right after the “enough” reflection) then I’m more motivated to add some tasks for that goal to the plan.

Also, I can see these values during the weekly review, but I don’t review that too closely unless there’s a negative trend that needs addressing.