可以根据json路径更新json的JavaScript库或模块是什么?
What is the JavaScript library or module that can update json based on jsonpath?
我以前使用过一些 Java 库来实现这一点。我认为,由于 json 是 Java 脚本到 Java 的固有特性,因此应该很容易找到一个可以做到这一点的库。但令人惊讶的是没有。
我寻找的一个例子是:给定一个 Json 对象:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
和一个 Json 路径,例如:
'$..author'
我可以调用一个 JavaScript 函数,它将 JsonObject、JsonPath 和 newValue(莎士比亚)作为输入参数,并更改所有出现的值JSONPATH 为新值,例如至
{
"store": {
"book": [
{
"category": "reference",
"author": "Shakespeare",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Shakespeare",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Shakespeare",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "Shakespeare",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
有什么建议吗?
尝试使用jsonpath npm 包。
使用apply 方法更改JSON 对象和returns 节点的值。
您需要添加一些方法,根据每个节点的路径更改值。
const _ = require('lodash');
var jp = require('jsonpath');
const data = {
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
var nodes = jp.apply(data, '$..author', function(value) { return value.toUpperCase() });
// [
// { path: ['$', 'store', 'book', 0], value: 'NIGEL REES' },
// { path: ['$', 'store', 'book', 1], value: 'EVELYN WAUGH' },
// { path: ['$', 'store', 'book', 2], value: 'HERMAN MELVILLE' },
// { path: ['$', 'store', 'book', 3], value: 'J. R. R. TOLKIEN' }
// ]
function chnageValueByPath(object, path, value) {
if(Array.isArray(path) && path[0] === '$') {
const pathWithoutFirstElement = path.slice(1);
_.set(object, pathWithoutFirstElement, value);
}
}
function changeValuesByPath(object, nodes, lastPropertyName) {
nodes.forEach((node)=> {
chnageValueByPath(object, node.path.concat(lastPropertyName), node.value);
})
return object;
}
const result = changeValuesByPath(data, nodes, 'author');
console.log(result);
我以前使用过一些 Java 库来实现这一点。我认为,由于 json 是 Java 脚本到 Java 的固有特性,因此应该很容易找到一个可以做到这一点的库。但令人惊讶的是没有。
我寻找的一个例子是:给定一个 Json 对象:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
和一个 Json 路径,例如:
'$..author'
我可以调用一个 JavaScript 函数,它将 JsonObject、JsonPath 和 newValue(莎士比亚)作为输入参数,并更改所有出现的值JSONPATH 为新值,例如至
{
"store": {
"book": [
{
"category": "reference",
"author": "Shakespeare",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Shakespeare",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Shakespeare",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "Shakespeare",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
有什么建议吗?
尝试使用jsonpath npm 包。
使用apply 方法更改JSON 对象和returns 节点的值。 您需要添加一些方法,根据每个节点的路径更改值。
const _ = require('lodash');
var jp = require('jsonpath');
const data = {
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
var nodes = jp.apply(data, '$..author', function(value) { return value.toUpperCase() });
// [
// { path: ['$', 'store', 'book', 0], value: 'NIGEL REES' },
// { path: ['$', 'store', 'book', 1], value: 'EVELYN WAUGH' },
// { path: ['$', 'store', 'book', 2], value: 'HERMAN MELVILLE' },
// { path: ['$', 'store', 'book', 3], value: 'J. R. R. TOLKIEN' }
// ]
function chnageValueByPath(object, path, value) {
if(Array.isArray(path) && path[0] === '$') {
const pathWithoutFirstElement = path.slice(1);
_.set(object, pathWithoutFirstElement, value);
}
}
function changeValuesByPath(object, nodes, lastPropertyName) {
nodes.forEach((node)=> {
chnageValueByPath(object, node.path.concat(lastPropertyName), node.value);
})
return object;
}
const result = changeValuesByPath(data, nodes, 'author');
console.log(result);