To achieve maximum performance, avoid using simdjson.loads() or simdjson.load() if you only need specific parts of a JSON document. The majority of overhead in Python JSON libraries comes from creating Python objects for the entire document.
Instead, use one of these two methods to access specific data without parsing the whole structure into Python objects:
- Indexing/Subscripting: Access elements directly via keys or indices (e.g.,
doc['key'][0]). - JSON Pointers: Use the
at_pointer() method with a JSON pointer string (e.g., doc.at_pointer('/path/to/element')).
Both methods are significantly faster because they avoid the overhead of constructing Python objects for the parts of the document you ignore.
import simdjson
parser = simdjson.Parser()
doc = parser.parse(b'{"res": [{"name": "first"}, {"name": "second"}]}')
# Method 1: Subscripting
assert doc['res'][1]['name'] == 'second'
# Method 2: JSON Pointers
assert doc.at_pointer('/res/1/name') == 'second'