Whole file
sean2077/jsonpath-python
The author described this change as “fix: resolve issue #16 support quoted keys in filters and paths”. It counts as a record because the check below fails on the code as it stood at 47bba2512 and passes on 6caa9d4e0, with nothing else changed between the two runs.
Projectsean2077/jsonpath-python
Fix saved2025-11-23
Sharing licenceMIT · LICENSE
Change size+13 −6
What the code was meant to do, written into the code itself as a save note
fix: resolve issue #16 support quoted keys in filters and paths
The change
| 118 | 118 | return f"#Q{n}" | |
| 119 | 119 | ||
| 120 | 120 | def _put_quote(self, m): | |
| 121 | - | return self.subx["#Q"][int(m.group(1))] | |
| 121 | + | return f"'{self.subx['#Q'][int(m.group(1))]}'" | |
| 122 | 122 | ||
| 123 | 123 | def _get_backquote(self, m): | |
| 124 | 124 | n = len(self.subx["#BQ"]) | |
| ⋯ | |||
| 146 | 146 | ||
| 147 | 147 | @staticmethod | |
| 148 | 148 | def _gen_obj(m): | |
| 149 | + | content = m.group(1) or m.group(2) # group 2 is for len() | |
| 149 | 150 | ret = "__obj" | |
| 150 | - | for e in m.group(1).split("."): | |
| 151 | + | for e in content.split("."): | |
| 152 | + | if len(e) >= 2 and ((e[0] == "'" and e[-1] == "'") or (e[0] == '"' and e[-1] == '"')): | |
| 153 | + | e = e[1:-1] | |
| 151 | 154 | ret += f'["{e}"]' | |
| 152 | 155 | return ret | |
| 153 | 156 | ||
| ⋯ | |||
| 239 | 242 | return | |
| 240 | 243 | ||
| 241 | 244 | # get value from dict | |
| 242 | - | if isinstance(obj, dict) and step in obj: | |
| 243 | - | if re.match(r"^\w+$", step): | |
| 244 | - | self._trace(obj[step], i + 1, f"{path}.{step}") | |
| 245 | + | step_key = step | |
| 246 | + | if len(step) >= 2 and step[0] == "'" and step[-1] == "'": | |
| 247 | + | step_key = step[1:-1] | |
| 248 | + | ||
| 249 | + | if isinstance(obj, dict) and step_key in obj: | |
| 250 | + | if re.match(r"^\w+$", step_key): | |
| 251 | + | self._trace(obj[step_key], i + 1, f"{path}.{step_key}") | |
| 245 | 252 | else: | |
| 246 | - | self._trace(obj[step], i + 1, f"{path}['{step}']") | |
| 253 | + | self._trace(obj[step_key], i + 1, f"{path}['{step_key}']") | |
| 247 | 254 | return | |
| 248 | 255 | ||
| 249 | 256 | # slice | |
The check that tells the two apart
fail→pass·tests/test_issues.py::test_issue_16_quoted_keys
Check file tests/test_issues.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it47bba251233b8f96963dc22e2e83238b417e332a
Broken version dated2025-11-23
Modulejsonpath.jsonpath
Units changedJSONPath
Fingerprint5ad8c686f74de7f6
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.