Skip to content

Question about efficiency (lazy loading) #111

@stijndcl

Description

@stijndcl

This issue is more of a question about the way the library works. If the answer is that this is currently not supported, consider it a feature request instead.

JsonPath::query() seems to return a Vec<Value>, which to me suggests that it collects all results from a query at once and returns that.

When querying very large files (e.g. a 100 GB Json file that doesn't fit in memory) or querying small amounts of data, this is not always ideal. For example, if I only want the first or second element, there's no point in storing all the others, or even trying to query them. Similarly, if I want to fetch all the items, it'd be useful to be able to loop over an Iterator<Value> that progressively retrieves more as I request them.

Does this library have support for such queries? I'm essentially looking for a LazyJsonPath or JsonPath::lazy_query() or whatever that only fetches the results I actually want, only when I request them, rather than everything upfront.

Examples

Example 1: I only want the first element, so I'd expect query() to stop immediately once it finds a result.

let very_large_json = json!([]); // Imagine this is a 100 GB list

// This should not fetch more than 1 result
JsonPath("$..name").query(&very_large_json).first()

Example 2: I only want the second element, so I'd expect query() to stop immediately once it finds two results. It should not store the first but just skip over it and increase a counter, as it is pointless to me anyways.

let very_large_json = json!([]); // Imagine this is a 100 GB list

// This should not store more than 1 result in memory, and not fetch more than 2
JsonPath("$..name").query(&very_large_json).get(1)

Example 3: I want all results, but they don't fit in memory all at once, so they should be streamed instead.

let very_large_json = json!([]); // Imagine this is a 100 GB list, streamed from a file instead of held in memory

// This should not fetch everything at once
for item in JsonPath("$..name").query(&very_large_json) {
    // do something
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions