Crates.io | jsonposition |
lib.rs | jsonposition |
version | 0.1.0 |
source | src |
created_at | 2024-11-24 23:43:44.386065 |
updated_at | 2024-11-24 23:43:44.386065 |
description | Finds a path to a JSON element at a provided string index |
homepage | |
repository | https://github.com/commonkestrel/jsonposition |
max_upload_size | |
id | 1459733 |
size | 9,898 |
JSON Position is json library for finding the path to JSON at an index in the original string. Similar to extensions in most IDEs to get path to cursor position.
use jsonposition::{ path, dot_path, Index };
let json = r#"[9, {"name": "b", "fields": [null, null, 87, 4], "path": "file.txt"}]"#;
let position = json.find("87").unwrap();
let vec_path = path(json, position).expect("Invalid JSON");
assert_eq!(vec_path, [Index::Array(1), Index::Object("fields".to_string()), Index::Array(2)]);
let dotted = dot_path(json, position).expect("Invalid JSON");
assert_eq!(dotted, "$.1.fields.2");
In this example we start with the raw JSON string [9, {"name": "b", "fields": [null, null, 87, 4]}]
We are trying to find the path to the first 87
contained in the string, which starts at char 42.
This can be indexed to with the path json[1]["fields"][2]
The path
function returns this path in a Vec<Index>
. Index
is an enum
with two varients. One is Array
, which is an index into an array, and Object
which is a key in an object.
The dot_path
function returns this path in a format used by most JsonPath libraries: "$.1.fields.2"