h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
572 stars 85 forks source link

Can't use filter or set index #89

Closed visionir-io closed 9 months ago

visionir-io commented 2 years ago

version 1.5.3 given this code:

import json,sqlite3,re
from jsonpath_ng import jsonpath
from jsonpath_ng.ext import parse
datapath = '/Users/mac/Documents/DSA Project/Resources/game_data_in_dsa.json'
with open(datapath, 'r') as ofile:
    data = json.load(ofile)

main_path = "$.units.[0].ID"

for match in parse(main_path).find(data):
    print(f'Path:{match.path}\nFull Path: {match.full_path}\n') #Value: {match.value}')
  "37106888302811": {
            "ID": 37106888302811,
            "NameKey": "THE_BEAST_UNIT_NAME",
            "UnitStats": {
                "Offense": 42,
                "Defense": 25,
                "Health": 360,
                "Recovery": 0,
                "CritChance": 0.05,
                "CritPower": 1.5,
                "AttackSpeed": 0.16,
                "Speed": 90,
                "Potency": 0.0,
                "Tenacity": 0.05,
                "Accuracy": 0.0,
                "Evasion": 0.0
            },
            "StatProgressionTable": 3784750133994731,
            "GearSlotTable": 3,
            "GearTiers": [
                {
                    "GearTierSlot": [
                        {
                            "Offense": true,
                            "Defense": false,
                            "Health": false,
                            "Recovery": false,
                            "CritChance": false,
                            "CritPower": false,
                            "AttackSpeed": false,
                            "ItemID": 5800404643593210,
                            "UnitLevel": 5,
                            "Speed": false,
                            "Potency": false,
                            "Tenacity": false,
                            "Accuracy": false,
                            "Evasion": false
                        },

my query returns the error:

  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jsonpath_ng/jsonpath.py", line 625, in _find_base
    return [DatumInContext(datum.value[self.index], path=self, context=datum)]
KeyError: 0

In the original file there are many instances like I provided and when I go with wildcard It works fine and I can go for each one

michaelmior commented 1 year ago

Have you tried using $.units[0].ID instead?