def parse_json_path(path):
<EOL>original_path = path<EOL>steps = []<EOL>failed = [<EOL>JsonStep(<EOL>type="<STR_LIT:object>",<EOL>key=original_path,<EOL>last=True,<EOL>failed=True,<EOL>)<EOL>]<EOL>digit_re = re.compile(r'<STR_LIT>')<EOL>key_re = re.compile(r'<STR_LIT>')<EOL>parts = path.split("<STR_LIT:[>")<EOL>first_key = parts[<NUM_LIT:0>]<EOL>if parts[<NUM_LIT:1>:]:<EOL><INDENT>path = "<STR_LIT:[>" + "<STR_LIT:[>".join(parts[<NUM_LIT:1>:])<EOL><DEDENT>else:<EOL><INDENT>path = "<STR_LIT>"<EOL><DEDENT>steps.append(JsonStep(<EOL>type="<STR_LIT:object>",<EOL>key=first_key,<EOL>))<EOL>if not path:<EOL><INDENT>steps[-<NUM_LIT:1>].last = True<EOL>return steps<EOL><DEDENT>while path:<EOL><INDENT>if path[:<NUM_LIT:2>] == "<STR_LIT>":<EOL><INDENT>path = path[<NUM_LIT:2>:]<EOL>steps.append(JsonStep(<EOL>type="<STR_LIT>",<EOL>key=<NUM_LIT:0>,<EOL>))<EOL>continue<EOL><DEDENT>digit_match = digit_re.match(path)<EOL>if digit_match:<EOL><INDENT>path = digit_re.sub("<STR_LIT>", path)<EOL>steps.append(JsonStep(<EOL>type="<STR_LIT>",<EOL>key=int(digit_match.group(<NUM_LIT:1>)),<EOL>))<EOL>continue<EOL><DEDENT>key_match = key_re.match(path)<EOL>if key_match:<EOL><INDENT>path = key_re.sub("<STR_LIT>", path)<EOL>steps.append(JsonStep(<EOL>type="<STR_LIT:object>",<EOL>key=key_match.group(<NUM_LIT:1>),<EOL>))<EOL>continue<EOL><DEDENT>return failed<EOL><DEDENT>next_step = None<EOL>for step in reversed(steps):<EOL><INDENT>if next_step:<EOL><INDENT>step.next_type = next_step.type<EOL><DEDENT>else:<EOL><INDENT>step.last = True<EOL><DEDENT>next_step = step<EOL><DEDENT>return steps<EOL>
Parse a string as a JSON path
An implementation of "steps to parse a JSON encoding path"
http://www.w3.org/TR/html-json-forms/#dfn-steps-to-parse-a-json-encoding-path