Skip to content

get()

Read a single value at a dot or bracket path without fully walking the document in PHP.

Signature

php
JsonFast::get(
    string $json,
    string $path,
    ?int $output = OUTPUT_ARRAY
): mixed|null

Parameters

NameTypeDefaultDescription
$jsonstringJSON document
$pathstringDot/bracket path (see syntax below)
$output?intOUTPUT_ARRAYOutput mode

Path syntax

PatternExampleNotes
Dot notationuser.nameNested object keys
Array indexusers[0].emailNumeric index in brackets
Wildcardusers[*].emailNot supported in get() — use search()

Returns

The value at $path, or null if the path does not exist.

WARNING

null is ambiguous — it can mean “missing path” or “JSON null at this path”. Use has() when you need to distinguish.

Examples

php
$json = '{"user":{"name":"Allan","profile":{"city":"Milton Keynes"}}}';

JsonFast::get($json, 'user.profile.city');  // "Milton Keynes"
JsonFast::get($json, 'user.name');          // "Allan"
JsonFast::get($json, 'user.missing');       // null

Array index:

php
JsonFast::get('{"items":[{"id":1},{"id":2}]}', 'items[1].id'); // 2

String output for a subtree:

php
$subtree = JsonFast::get($json, 'user.profile', JsonFast::OUTPUT_STRING);

Native tools, weird experiments, and practical performance work.