39 lines
876 B
JavaScript
Raw Normal View History

2019-04-13 22:27:33 +09:00
import * as jsonpath from "jsonpath-wasm";
let jsonObj = {
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
};
let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
2019-06-23 23:59:18 +09:00
const path = '$..friends[0]';
let ret1 = jsonpath.select(jsonObj, path);
let ret2 = jsonpath.compile(path)(jsonObj);
let ret3 = jsonpath.selector(jsonObj)(path);
2019-04-13 22:27:33 +09:00
let selector = new jsonpath.Selector();
2019-06-23 23:59:18 +09:00
selector.path(path);
2019-04-13 22:27:33 +09:00
selector.value(jsonObj);
2019-06-23 23:59:18 +09:00
let ret4 = selector.select();
2019-04-13 22:27:33 +09:00
console.log(
2019-06-23 23:59:18 +09:00
JSON.stringify(ret) == JSON.stringify(ret1),
JSON.stringify(ret) == JSON.stringify(ret2),
JSON.stringify(ret) == JSON.stringify(ret3),
JSON.stringify(ret) == JSON.stringify(ret4)
2019-04-13 22:27:33 +09:00
);
2019-06-23 23:59:18 +09:00