h2non / jsonpath-ng

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

specifying a comma-separated list of array indexes throws an Exception #28

Open christopheraranda opened 4 years ago

christopheraranda commented 4 years ago

According to the README, a comma-separated list of array indexes may be specified in a JsonPath.

Syntax Meaning
[n] array index (may be comma-separated list)

However, when attempting to use multiple indexes, the following exception is thrown:

def p_error(self, t):
>       raise Exception('Parse error at %s:%s near token %s (%s)' % (t.lineno, t.col, t.value, t.type))
E       Exception: Parse error at 1:14 near token , (,)

The following code can be used to reproduce the issue:

import unittest

from jsonpath_ng.ext import parse

TEST_DATA = {
    'store': {
        'book': [
            {
                'category': 'reference',
                'author': 'Nigel Rees',
                'title': 'Sayings of the Century',
                'price': 8.95
            },
            {
                'category': 'fiction',
                'author': 'Evelyn Waugh',
                'title': 'Sword of Honour',
                'price': 12.99
            },
            {
                'category': 'fiction',
                'author': 'Herman Melville',
                'title': 'Moby Dick',
                'isbn': '0-553-21311-3',
                'price': 8.99
            }
        ],
        'bicycle': {
            'color': 'red',
            'price': 19.95
        }
    }
}

class Test(unittest.TestCase):

    def test(self):
        path = "$.store.book[0,2].title"
        values = [match.value for match in parse(path).find(TEST_DATA)]
        self.assertEqual(values, ['Sayings of the Century', 'Moby Dick'])

I have verified that the path is valid on various JsonPath evaluators.