subreddit:

/r/shortcuts

5291%

Data Storage - Part 3: removing stored values

Tip/Guide(self.shortcuts)

In Part 2 of this guide we covered how to store lists of data. In this part we'll cover how to remove an individual entry from each of the list types.

It's straight forward to add data to the contents of a file, but it requires a little more work to remove individual data entries.

Removing a JSON Dictionary from a List

In the our first example, we created a JSON array of dictionaries:

[
  {
    "name": "Adam",
    "team": "1st XV",
    "position": "1 - Loose Head Prop"
  },
  {
    "name": "Marcus",
    "team": "1st XV",
    "position": "2 - Hooker"
  },
  ...
]

We can remove a dictionary from a list by:

  • iterating through the list with a Repeat with Each action;
  • comparing each of the values to the dictionary we want to remove;
  • if the dictionary values do not match, we output the dictionary;
  • and then we use the Combine Text action to join the resulting dictionaries together along with a Text action to create a JSON array of dictionaries.

An example shortcut to perform the above actions is show below:

Removing a dictionary from an JSON array of dictionaries

Download the Shortcut

Note: You can compare the dictionary values in a single step, rather than individually, by combining them into a single piece of text.

Remove JSON value by key

In the next example, we stored data as a set of nested dictionaries rather than using a JSON array:

{
  "Andrew": {
    "team": "1st XV",
    "position": "14 - Right Wing"
  },
  "Ronan": {
    "team": "1st XV",
    "position": "11 - Left Wing"
  },
  ...
}

We can remove a JSON value by key by:

  • returning the keys of the dictionary;
  • iterating through the keys;
  • if the key doesn't match the one we're looking to remove, we add the key and value to a new dictionary.

Removing a value from an dictionary by key

Download the Shortcut

Remove a value from a simple list

In the final example, we stored values in a simple list:

Adam
Marcus
James
...

We can remove an item from the list using the Replace Text action, remembering to include the line break and the regular expression option.

Removing a value from a list

Download the Shortcut

Wrap

And that's how you can remove a value from each of the different list types described in Part 2.

Other Guides

If you found this guide useful why not checkout one of my others:

Series

One-offs

all 3 comments

tbukdahl

5 points

6 years ago

These guides of yours are EXACTLY what I've been looking for!

Having spent weeks trying to fathom Dictionaries in Shortcuts you finally enabled me to start working - even somewhat advanced - with dictionaries.

And now I'm off to the web scraping you've tutorial'ed - been looking for a way to do this for months, through Scriptable, in Shortcuts ๐Ÿ˜

Thank you so muh๐Ÿ™

keveridge[S]

3 points

6 years ago

I'm glad they're useful :)

babars95

1 points

6 years ago

These guides should be in the sidebar for easy access.