# JSON.ARRINDEX

> Find index of value in array.

Use `JSON.ARRINDEX` to find the first position of a value inside the arrays a path selects.

The value is JSON text and is compared for exact equality, so `1` does not match `"1"`. The optional `start` and `stop` bound the search: `start` is inclusive, `stop` is exclusive, both may be negative to count from the end of the array, and `0` for `stop` means "to the end". The reply is the index of the first match or `-1` when the value is not present, with one result per array the path matched.

## Syntax

```redis
JSON.ARRINDEX <key> <path> <value> [start [stop]]
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `key` | Yes | No | JSON document key. |
| `path` | Yes | No | Path selecting arrays. |
| `value` | Yes | No | Valid JSON value to locate. |
| `start` | No | No | Inclusive starting index. |
| `stop` | No | No | Exclusive ending index. |

## Important points

- Raw TCP examples pass JSON values as valid JSON text. Typed Upstash SDK helpers serialize native objects and values for you.
- Paths beginning with `$` use JSONPath and can match multiple values, so many JSON commands return an array of per-match results.

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply |
| --- | --- |
| RESP2 | Array of integer indexes or null values, one per matched path |
| RESP3 | Array of integer indexes or null values, one per matched path |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>

<Accordion title="Redis CLI" icon="terminal">

```bash
JSON.ARRINDEX profile $.tags '"new"'
```

</Accordion>

<Accordion title="@upstash/redis" icon="node-js" iconType="brands">

```ts
import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv();

const index = await redis.json.arrindex("key", "$.path.to.array", "a");
```

</Accordion>

<Accordion title="upstash_redis" icon="python" iconType="brands">

```python
from upstash_redis import Redis

redis = Redis.from_env()
result = redis.json().arrindex("profile", "$.tags", "new")
print(result)
```

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);
const result = await redis.call("JSON.ARRINDEX", "profile", "$.tags", "\"new\"");
console.log(result);
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const client = await createClient({ url: process.env.REDIS_URL })
  .on("error", console.error)
  .connect();
const result = await client.json.arrIndex("profile", "$.tags", "new");
console.log(result);
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
result = client.json().arrindex("profile", "$.tags", "new")
print(result)
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/redis/go-redis/v9"
)

func main() {
    opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
    if err != nil {
        panic(err)
    }
    client := redis.NewClient(opts)
    result, err := client.JSONArrIndex(context.Background(), "profile", "$.tags", "new").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}
```

</Accordion>

<Accordion title="jedis" icon="java" iconType="brands">

```java
import java.net.URI;

import redis.clients.jedis.JedisPooled;

try (JedisPooled jedis = new JedisPooled(new URI(System.getenv("REDIS_URL")))) {
  Object result = jedis.jsonArrIndex("profile", new redis.clients.jedis.json.Path("$.tags"), "new");
  System.out.println(result);
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
use redis::JsonCommands;

fn main() -> redis::RedisResult<()> {
    let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
    let client = redis::Client::open(url)?;
    let mut connection = client.get_connection()?;

    let result: redis::Value = connection.json_arr_index("profile", "$.tags", &"new")?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
