> For the complete documentation index, see [llms.txt](https://upsolver.gitbook.io/content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://upsolver.gitbook.io/content/reference-1/functions-and-operators/functions/array/value_index_in_array.md).

# VALUE\_INDEX\_IN\_ARRAY

Calculates a 1-based positional index for each non-null value within a sub-array, resetting the index with each new sub-array. This is particularly useful for maintaining context-specific indexes within nested array structures.

## Syntax

```sql
VALUE_INDEX_IN_ARRAY(array)
```

## Arguments

#### **`array`**

Type: any

An array of any value types.

***

## Example

### Transformation job example

In this example, column **orders** contains the following two rows:

{% code overflow="wrap" fullWidth="false" %}

```
[{"products":[{"name":"Apple"},{"name":"Banana"}]},{"products":[{"name":"Cherry"},{"name":"Cherry"}]}]
[{"products":[{"name":"Apple","type":"Fruit"},{"name":"Banana"}]},{"products":[{"name":"Cherry"},{"name":"Cherry"},{"type":"Berry"}]}]
```

{% endcode %}

If we use this in the following job, it will output the query result below:

```sql
CREATE JOB demo_item_index
   RUN_INTERVAL = 1 MINUTE
   START_FROM = BEGINNING
   ADD_MISSING_COLUMNS = TRUE
AS INSERT INTO default_glue_catalog.upsolver_samples.orders_transformed_data 
 MAP_COLUMNS_BY_NAME
  SELECT
   *,
   VALUE_INDEX_IN_ARRAY(orders[].products[].name) AS orders[].products[].name_index,
   VALUE_INDEX_IN_ARRAY(orders[].products[].type) AS orders[].products[].type_index
  FROM default_glue_catalog.upsolver_samples.orders_raw_data
  WHERE time_filter();
```

**Query result**

The `VALUE_INDEX_IN_ARRAY` function calculates the positional index of the value in the **products** array and returns the following result:

{% code overflow="wrap" fullWidth="false" %}

```
[{"products":
[{"name":"Apple","name_index":1},{"name":"Banana","name_index":2}]},{"products":[{"name":"Cherry","name_index":1},{"name":"Cherry","name_index":2}]
}]
[{"products":
[{"name":"Apple","name_index":1,"type":"Fruit","type_index":1},{"name":"Banana","name_index":2}]},{"products":[{"name":"Cherry","name_index":1},{"name":"Cherry","name_index":2},{"type":"Berry","type_index":1}]}]
```

{% endcode %}

Unlike [`VALUE_INDEX_IN_ROW`](/content/reference-1/functions-and-operators/functions/array/value_index_in_row.md), `VALUE_INDEX_IN_ARRAY` resets the count for each **products** array. Each **product** is a separate context that provides an index for the position of **name** or **type** field in that specific array.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://upsolver.gitbook.io/content/reference-1/functions-and-operators/functions/array/value_index_in_array.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
