# JSON array index by

`json-array-index-by` · version 1.0.0 · JSON & JSONL · free, no key needed

Convert an array of objects into a single object indexed by a specific key.

**Use when you need to: json array index by · index json array by key · convert array to map by key.**

## Supported

- json array index by
- index json array by key
- convert array to map by key

## Not supported

- multiple keys
- deep indexing

## Behavior

- Creates a single JSON object where keys are the stringified values of the specified index field.
- If multiple objects have the same key, the behavior is determined by on_duplicate ("last", "first", or "throw").
- Result keys are ordered by first appearance.
- Missing or null keys are treated as the string "undefined" or "null".

## Input

- `array` (array of object, required): max items 10000
- `key` (string, required): min length 1
- `on_duplicate` (one of "last", "first", "throw", optional): default `"last"`

## Output

- `result` (object, required)
- `count` (integer, required)

## Limits

- max array items: 10000
- max output items: 10000

## Example

Request input:

```json
{
  "array": [
    {
      "id": "a",
      "val": 1
    },
    {
      "id": "b",
      "val": 2
    },
    {
      "id": "a",
      "val": 3
    }
  ],
  "key": "id",
  "on_duplicate": "last"
}
```

Response:

```json
{
  "result": {
    "result": {
      "a": {
        "id": "a",
        "val": 3
      },
      "b": {
        "id": "b",
        "val": 2
      }
    },
    "count": 2
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "json-array-index-by",
  "version": "1.0.0",
  "input": {
    "array": [
      {
        "id": "a",
        "val": 1
      },
      {
        "id": "b",
        "val": 2
      },
      {
        "id": "a",
        "val": 3
      }
    ],
    "key": "id",
    "on_duplicate": "last"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/json-array-index-by/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"array":[{"id":"a","val":1},{"id":"b","val":2},{"id":"a","val":3}],"key":"id","on_duplicate":"last"}'
```

The machine-readable contract is at [/v1/tools/json-array-index-by/versions/1.0.0](/v1/tools/json-array-index-by/versions/1.0.0).

### CLI

```sh
node cli.mjs run json-array-index-by 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Array to JSONL](/tools/array-to-jsonl): Serialize an array of JSON-safe values as JSON Lines text.
- [JSON array distinct by](/tools/json-array-distinct-by): Deduplicate a JSON array of objects based on specific keys.
- [JSON array group count](/tools/json-array-group-count): Group an array of JSON objects by exact keys and return the count.
- [JSONL to array](/tools/jsonl-to-array): Parse strict JSON Lines text into an array of JSON-safe values.
- [JSON array anti join](/tools/json-array-anti-join): Anti join two JSON arrays of objects on exact keys.
- [JSON array filter equals](/tools/json-array-filter-equals): Filter JSON array objects where a key equals a specific value.
