Loading

Back to Home

$jsonArrayIndex

$jsonArrayIndex returns the index of a given value inside a JSON array. Returns -1 if the value is not found.

Syntax

$jsonArrayIndex[Key;...;Value]

Returns empty if no $jsonParse or $jsonSet was called, or $jsonClear was called.

What happens

  1. $jsonParse loads the JSON object.
  2. $jsonArrayIndex searches the array at the given key path for the value.
  3. Returns the numeric index (starting at 0) if found, or -1 if not.

Example 1: Find a value in an array

$nomention
$jsonParse[{"fruits":["apple","banana","orange"\]}]
$jsonArrayIndex[fruits;banana]

What happens:

  1. $jsonParse loads the object.
  2. $jsonArrayIndex[fruits;banana] finds banana at index 1 and returns it.

Output:

1

Example 2: Search for user input

$nomention
$jsonParse[{"roles":["admin","mod","member"\]}]
Index: $jsonArrayIndex[roles;$message]

What happens:

  1. $jsonParse loads the object.
  2. $jsonArrayIndex searches for whatever the user typed.

If the user runs !example mod:

Index: 1

If the user runs !example owner:

Index: -1

Common uses

See also