cerbero90 / json-parser

🧩 Zero-dependencies lazy parser to read JSON of any dimension and from any source in a memory-efficient way.
MIT License
671 stars 11 forks source link

Multilevel Item skipping and getting deeply nested objects in memory directly #3

Closed bhushan closed 6 months ago

bhushan commented 8 months ago

Detailed description

Getting multiple nested content dynamically.

Context

$json = JsonParser::parse($source)->pointer('/results/-/gender');

foreach ($json as $key => $value) {
    // 1st iteration: $key === 'gender', $value === 'female'
    // 2nd iteration: $key === 'gender', $value === 'female'
    // 3rd iteration: $key === 'gender', $value === 'male'
    // and so on for all the objects in the array...
}

Same like above example we can skip any element keys after results, but we cant do that on multiple level

- $json = JsonParser::parse($source)->pointer('/results/-/gender');
+ $json = JsonParser::parse($source)->pointer('/results/-/-/gender');
bhushan commented 8 months ago

I understood, skipping key is only possible on array, how can we make it work with object as well?

cerbero90 commented 6 months ago

Hi @bhushan, if you need to extract entire objects, you can simply use the /results/- JSON pointer