JSON.TOGGLE

Since 1.1.0JSONChanged in 1.2.1

Syntax

JSON.TOGGLE key path
  • Flags: write
  • ACL categories: @json, @slow, @write
  • Arity: 3

History

  • 1.2.1: arity: -2 -> 3

Flips each boolean path matches in the JSON document at key: true becomes false and false becomes true. Use it for flags such as active or muted without reading the value first. The path is required; to toggle a document that is a single boolean, give $.

A JSONPath flips every boolean it matches and leaves other values alone; a legacy path flips the first match only. See JSON paths.

Reply

  • A JSONPath: an array with each new value as 1 (true) or 0 (false), nil where a match is not a boolean; an empty array when nothing matches.
  • A legacy path: the new value as the bulk string "true" or "false"; nil when the path matches nothing.
  • nil if the key does not exist.

Errors

  • ERR path is not a boolean, for a legacy path.

Examples

> JSON.SET t $ '{"active":true,"x":1,"n":{"active":false}}'
OK
> JSON.TOGGLE t $.active
1) (integer) 0
> JSON.TOGGLE t .active
"true"
> JSON.TOGGLE t $..active
1) (integer) 0
2) (integer) 1
> JSON.TOGGLE t $.x
1) (nil)
> JSON.TOGGLE t .x
(error) ERR path is not a boolean
> JSON.GET t
"{\"active\":false,\"x\":1,\"n\":{\"active\":true}}"