要使用正则表达式过滤的JsonPath表达式

问题描述 投票:8回答:3

[我们正在使用使用jayway库评估JSONpath表达式的工具。 似乎无法使用Javascript。在这种情况下,如何在JSONPath中使用正则表达式。例如,在下面的示例中,我想过滤所有书名中带有单词“ Sword”的书名:

{
    "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
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}
regex json jsonpath
3个回答
7
投票
$.store.book[?(@.title =~ /^.*Sword.*$/)]

忽略大小写:

$.store.book[?(@.title =~ /^.*sword.*$/i)]

0
投票
"title":\s*"([^"]*\bSword\b[^"]*)"

如有必要,添加不区分大小写的修饰符i。从组索引1中获取标题字符串。

DEMO

0
投票
$.store.book[?(/^.*sword.*$/i.test(@.title))]

请在这里查看https://github.com/jpaquit/jsonpath/tree/0.8.5-+-regexp用于JS lib中的“ =〜”语法。

© www.soinside.com 2019 - 2024. All rights reserved.