# function_selector_json

Builds the canonical signature for a JSON ABI function fragment and returns its 4-byte Keccak-256 selector.

Canonical HTML: <https://determica.com/docs/functions/function_selector_json>

- Kind: scalar function
- Category: [Contracts and ABI](/docs/category/contracts-and-abi.md)
- Tags: ABI

Builds the canonical signature for a JSON ABI function fragment and returns its 4-byte Keccak-256 selector.

## Overload 1: function_selector_json(JSON)

```sql
function_selector_json(JSON)
```

Canonicalizes one JSON ABI function fragment, then hashes the derived signature.

- Kind: scalar
- Execution context: Offline
- Behavior: Canonical Json Function Selector
- Execution modes: Offline
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: Deterministic
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: None documented

### Inputs

- `abi` (JSON; required; positional) — A single function ABI object with name and inputs. Tuple and array structure is preserved while uint/int aliases are normalized to uint256/int256.

### Returns

Function selector

Returns the first four bytes of Keccak-256 over the canonical function signature as BYTES4.

- `selector` (BYTES4)

Function selector. Returns the first four bytes of Keccak-256 over the canonical function signature as BYTES4.

### Examples

_Local SQL · Local_

```sql
-- Selector of transfer(address,uint256) from its JSON ABI entry
SELECT function_selector_json(json('{
    "type": "function",
    "name": "transfer",
    "inputs": [
        {"name": "to", "type": "address"},
        {"name": "value", "type": "uint256"}
    ]
}')) AS selector;
```

## Guidance

### Choose a selector source

Use function_selector_json when the source of truth is a JSON ABI fragment. It preserves tuple and array structure and normalizes uint and int aliases before hashing.

- Use function_selector for signature text that is already canonical.
- Use encode_function_data when complete selector-prefixed calldata is required.
- Use event_signature for event topic0.

```sql
SELECT function_selector('transfer(address,uint256)') AS selector;
```

### Choose the right ABI primitive

Choose by byte framing and direction: selector construction, standard arguments, calldata, packed bytes, raw words, calldata decoding, and return-data decoding are distinct operations.

- function_selector hashes already-canonical signature text exactly; function_selector_json builds a canonical signature from one JSON ABI fragment first.
- encode_function_args emits selector-free standard ABI arguments; encode_function_data prefixes those same bytes with the 4-byte selector.
- abi_encode_packed concatenates compact scalar encodings without standard ABI offsets, lengths, or 32-byte padding; evm_abi_word always emits exactly one padded 32-byte word.
- decode_function_data consumes and validates selector-prefixed calldata inputs; call_decode consumes selector-free function return bytes.

```sql
SELECT
  function_selector('transfer(address,uint256)') AS selector,
  encode_function_data(
    '[{"type":"function","name":"transfer","inputs":[{"type":"address"},{"type":"uint256"}]}]'::JSON,
    'transfer(address,uint256)',
    '0x000000000000000000000000000000000000dEaD'::ADDRESS,
    1::UINT256
  ) AS calldata;
```

## Related functions

- [function_selector](/docs/functions/function_selector.md) — Alternative
- [encode_function_data](/docs/functions/encode_function_data.md) — See Also
- [encode_function_args](/docs/functions/encode_function_args.md) — See Also
- [raw_call](/docs/functions/raw_call.md) — Usually Before
