def add(parent, idx, value):
  """Add a value to a dict."""
  if isinstance(parent, dict):
    if idx in parent:
      raise JSONPatchError("Item already exists")
    parent[idx] = value
  elif isinstance(parent, list):
    if idx == "" or idx == "~":
      parent.append(value)
    else:
      parent.insert(int(idx), value)
  else:
    raise JSONPathError("Invalid path for operation")
Add a value to a dict.