Path returns the original GJSON path for a Result where the Result came from a simple path that returns a single value, like: gjson.Get(json, "friends.#(last=Murphy)") The returned value will be in the form of a JSON string: "friends.0" The param 'json' must be the original JSON used when call
(json string)
| 3461 | // when the Result came from a path that contained a multipath, modifier, |
| 3462 | // or a nested query. |
| 3463 | func (t Result) Path(json string) string { |
| 3464 | var path []byte |
| 3465 | var comps []string // raw components |
| 3466 | i := t.Index - 1 |
| 3467 | if t.Index+len(t.Raw) > len(json) { |
| 3468 | // JSON cannot safely contain Result. |
| 3469 | goto fail |
| 3470 | } |
| 3471 | if !strings.HasPrefix(json[t.Index:], t.Raw) { |
| 3472 | // Result is not at the JSON index as expected. |
| 3473 | goto fail |
| 3474 | } |
| 3475 | for ; i >= 0; i-- { |
| 3476 | if json[i] <= ' ' { |
| 3477 | continue |
| 3478 | } |
| 3479 | if json[i] == ':' { |
| 3480 | // inside of object, get the key |
| 3481 | for ; i >= 0; i-- { |
| 3482 | if json[i] != '"' { |
| 3483 | continue |
| 3484 | } |
| 3485 | break |
| 3486 | } |
| 3487 | raw := revSquash(json[:i+1]) |
| 3488 | i = i - len(raw) |
| 3489 | comps = append(comps, raw) |
| 3490 | // key gotten, now squash the rest |
| 3491 | raw = revSquash(json[:i+1]) |
| 3492 | i = i - len(raw) |
| 3493 | i++ // increment the index for next loop step |
| 3494 | } else if json[i] == '{' { |
| 3495 | // Encountered an open object. The original result was probably an |
| 3496 | // object key. |
| 3497 | goto fail |
| 3498 | } else if json[i] == ',' || json[i] == '[' { |
| 3499 | // inside of an array, count the position |
| 3500 | var arrIdx int |
| 3501 | if json[i] == ',' { |
| 3502 | arrIdx++ |
| 3503 | i-- |
| 3504 | } |
| 3505 | for ; i >= 0; i-- { |
| 3506 | if json[i] == ':' { |
| 3507 | // Encountered an unexpected colon. The original result was |
| 3508 | // probably an object key. |
| 3509 | goto fail |
| 3510 | } else if json[i] == ',' { |
| 3511 | arrIdx++ |
| 3512 | } else if json[i] == '[' { |
| 3513 | comps = append(comps, strconv.Itoa(arrIdx)) |
| 3514 | break |
| 3515 | } else if json[i] == ']' || json[i] == '}' || json[i] == '"' { |
| 3516 | raw := revSquash(json[:i+1]) |
| 3517 | i = i - len(raw) + 1 |
| 3518 | } |
| 3519 | } |
| 3520 | } |