Given the dictionary
product_data = {
'restrictions({"basketItems":[],"endDateTime":"","startDateTime":""})': [
{
'__typename': 'ProductRestrictionType',
'type': 'RdgDateRange',
'isViolated': False,
'message': 'Price Cuts - Was £0.50\n'
}, {
'__typename': 'ProductRestrictionType',
'type': 'ExcludedProduct',
'isViolated': True,
'message': 'This product is unavailable'
}
]
}
I wanted to select the following key
restrictions({"basketItems":[],"endDateTime":"","startDateTime":""})
I tried the following but I am getting an error due to brackets:
import jmespath
expression = jmespath.compile('restrictions({"basketItems":],"endDateTime":"","startDateTime":""})')
result = expression.search(product_data)
How can I fix this expression JMESPath?
As pointed in the specifications:
Source: https://jmespath.org/specification.html#identifiers
And given that
unquoted-stringisA-Za-z0-9_.In order to select a key in a JSON like
You would use the expression:
Yielding:
But since you also have quotes in the string, you would have to escape those quotes, e.g.:
Would be queried with:
And finally, since you are in a Python string, in order for the escaping character, backslash, to be a literal
\and not an escaping sequence, you'll have to double it:Full code, with an expression string split for readability:
Would yield: