How can I get discount value if brand_id=='983'.
如果brand_id =='983',我怎样才能获得折扣价值。
Sample JSON:
示例JSON:
{
"prods": [
{
"info": {
"rate": 100
},
"grocery": [
{
"brand": "A",
"brand_id": "983"
},
{
"brand": "B",
"brand_id": "253"
}
],
"discount": "20"
}
]
}
What I have tried till now is
我到现在为止尝试的是
$.prods[*].grocery[?(@.brand_id=='983')]
This returns me list/array of matched objects. But I am not able to traverse back into the tree. Any help on this?
这将返回匹配对象的列表/数组。但我无法回到树上。对此有何帮助?
2 个解决方案
#1
2
Indeed, JSONPath isn't very good at that, so I tackled this kind of problem with my own small library; so, here's a fiddle for your example:
实际上,JSONPath并不是很擅长,所以我用自己的小型库解决了这个问题;所以,这是你的例子的小提琴:
https://jsfiddle.net/YSharpLanguage/j9oetwnn/3
https://jsfiddle.net/YSharpLanguage/j9oetwnn/3
where:
哪里:
var products = {
"prods": [
{
"info": {
"rate": 85
},
"grocery": [
{
"brand": "C",
"brand_id": "984"
},
{
"brand": "D",
"brand_id": "254"
}
],
"discount": "15"
},
{
"info": {
"rate": 100
},
"grocery": [
{
"brand": "A",
"brand_id": "983"
},
{
"brand": "B",
"brand_id": "253"
}
],
"discount": "20"
}
]
};
function GroceryItem(obj) {
return (typeof obj.brand === "string") && (typeof obj.brand_id === "string");
}
// last parameter set to "true", to grab all the "GroceryItem" instances
// at any depth:
var itemsAndDiscounts = [ products ].nodeset(GroceryItem, true).
map(
function(node) {
var item = node.value, // node.value: the current "GroceryItem" (aka "$.prods[*].grocery[*]")
discount = node.parent. // node.parent: the array of "GroceryItem" (aka "$.prods[*].grocery")
parent. // node.parent.parent: the product (aka "$.prods[*]")
discount; // node.parent.parent.discount: the product discount
// finally, project into an easy-to-filter form:
return { id: item.brand_id, discount: discount };
}
),
discountOfItem983;
discountOfItem983 = itemsAndDiscounts.
filter
(
function(mapped) {
return mapped.id === "983";
}
)
[0].discount;
console.log("All items and discounts: " + JSON.stringify(itemsAndDiscounts, null, 2));
console.log("Discount of #983: " + discountOfItem983);
gives:
得到:
All items and discounts: [
{
"id": "984",
"discount": "15"
},
{
"id": "254",
"discount": "15"
},
{
"id": "983",
"discount": "20"
},
{
"id": "253",
"discount": "20"
}
]
Discount of #983: 20
Here are other examples / use cases:
以下是其他示例/用例:
JSON-to-some-markup
JSON transformations, revisited (XSLT look-alike)
重新审视JSON转换(XSLT外观相似)
(at: https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10)
(at:https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10)
JSON-to-JSON
Super-lightweight JSON-to-JSON transformations
超轻量级JSON-to-JSON转换
(at: https://jsfiddle.net/YSharpLanguage/ppfmmu15/10)
(at:https://jsfiddle.net/YSharpLanguage/ppfmmu15/10)
A JavaScript equivalent of...
XSLT 3.0 REC Section 14.4 Example: Grouping Nodes based on Common Values
XSLT 3.0 REC第14.4节示例:基于公共值对节点进行分组
(at: http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1)
(见:http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1)
Cf. https://www.w3.org/TR/xslt-30/#grouping-examples
参看https://www.w3.org/TR/xslt-30/#grouping-examples
A JavaScript equivalent of...
JSONiq Use Cases Section 1.1.2. Grouping Queries for JSON
JSONiq用例第1.1.2节。为JSON分组查询
(at: https://jsfiddle.net/YSharpLanguage/hvo24hmk/3)
(at:https://jsfiddle.net/YSharpLanguage/hvo24hmk/3)
Cf. http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
参看http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
'Hope this helps,
'希望这可以帮助,
#2
0
You can write a function that returns the parent node. Once you have that, you should develop a function that walks (traverses) all the object and all its nodes and arrays, and when it finds the desired Id, well, you just get the parent and retrieve for the discount.
您可以编写一个返回父节点的函数。一旦你有了这个,你应该开发一个遍历(遍历)所有对象及其所有节点和数组的函数,当它找到所需的Id时,你就得到父进程并检索折扣。
Here you have the simplest example of a function that returns the parent node:
这里有一个返回父节点的函数的最简单示例:
const myVar = {
"prods": [{
"info": {
"rate": 100
},
"grocery": [{
"brand": "A",
"brand_id": "983",
myParent: function() {
const that = this; // to fix the caller instead of using 'bind' or 'apply'
return that;
}
},
{
"brand": "B",
"brand_id": "253",
myParent: function() {
const that = this;
return that;
}
}
],
"discount": "20"
}]
}
function myFunction() {
let x = document.getElementById("myNumber").value;
let text = myVar.prods[0].grocery.find(el => el.brand_id === x).myParent().brand;
document.getElementById("demo").innerHTML = text;
}
<input type="number" id="myNumber" value="253">
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
#1
2
Indeed, JSONPath isn't very good at that, so I tackled this kind of problem with my own small library; so, here's a fiddle for your example:
实际上,JSONPath并不是很擅长,所以我用自己的小型库解决了这个问题;所以,这是你的例子的小提琴:
https://jsfiddle.net/YSharpLanguage/j9oetwnn/3
https://jsfiddle.net/YSharpLanguage/j9oetwnn/3
where:
哪里:
var products = {
"prods": [
{
"info": {
"rate": 85
},
"grocery": [
{
"brand": "C",
"brand_id": "984"
},
{
"brand": "D",
"brand_id": "254"
}
],
"discount": "15"
},
{
"info": {
"rate": 100
},
"grocery": [
{
"brand": "A",
"brand_id": "983"
},
{
"brand": "B",
"brand_id": "253"
}
],
"discount": "20"
}
]
};
function GroceryItem(obj) {
return (typeof obj.brand === "string") && (typeof obj.brand_id === "string");
}
// last parameter set to "true", to grab all the "GroceryItem" instances
// at any depth:
var itemsAndDiscounts = [ products ].nodeset(GroceryItem, true).
map(
function(node) {
var item = node.value, // node.value: the current "GroceryItem" (aka "$.prods[*].grocery[*]")
discount = node.parent. // node.parent: the array of "GroceryItem" (aka "$.prods[*].grocery")
parent. // node.parent.parent: the product (aka "$.prods[*]")
discount; // node.parent.parent.discount: the product discount
// finally, project into an easy-to-filter form:
return { id: item.brand_id, discount: discount };
}
),
discountOfItem983;
discountOfItem983 = itemsAndDiscounts.
filter
(
function(mapped) {
return mapped.id === "983";
}
)
[0].discount;
console.log("All items and discounts: " + JSON.stringify(itemsAndDiscounts, null, 2));
console.log("Discount of #983: " + discountOfItem983);
gives:
得到:
All items and discounts: [
{
"id": "984",
"discount": "15"
},
{
"id": "254",
"discount": "15"
},
{
"id": "983",
"discount": "20"
},
{
"id": "253",
"discount": "20"
}
]
Discount of #983: 20
Here are other examples / use cases:
以下是其他示例/用例:
JSON-to-some-markup
JSON transformations, revisited (XSLT look-alike)
重新审视JSON转换(XSLT外观相似)
(at: https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10)
(at:https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10)
JSON-to-JSON
Super-lightweight JSON-to-JSON transformations
超轻量级JSON-to-JSON转换
(at: https://jsfiddle.net/YSharpLanguage/ppfmmu15/10)
(at:https://jsfiddle.net/YSharpLanguage/ppfmmu15/10)
A JavaScript equivalent of...
XSLT 3.0 REC Section 14.4 Example: Grouping Nodes based on Common Values
XSLT 3.0 REC第14.4节示例:基于公共值对节点进行分组
(at: http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1)
(见:http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1)
Cf. https://www.w3.org/TR/xslt-30/#grouping-examples
参看https://www.w3.org/TR/xslt-30/#grouping-examples
A JavaScript equivalent of...
JSONiq Use Cases Section 1.1.2. Grouping Queries for JSON
JSONiq用例第1.1.2节。为JSON分组查询
(at: https://jsfiddle.net/YSharpLanguage/hvo24hmk/3)
(at:https://jsfiddle.net/YSharpLanguage/hvo24hmk/3)
Cf. http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
参看http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
'Hope this helps,
'希望这可以帮助,
#2
0
You can write a function that returns the parent node. Once you have that, you should develop a function that walks (traverses) all the object and all its nodes and arrays, and when it finds the desired Id, well, you just get the parent and retrieve for the discount.
您可以编写一个返回父节点的函数。一旦你有了这个,你应该开发一个遍历(遍历)所有对象及其所有节点和数组的函数,当它找到所需的Id时,你就得到父进程并检索折扣。
Here you have the simplest example of a function that returns the parent node:
这里有一个返回父节点的函数的最简单示例:
const myVar = {
"prods": [{
"info": {
"rate": 100
},
"grocery": [{
"brand": "A",
"brand_id": "983",
myParent: function() {
const that = this; // to fix the caller instead of using 'bind' or 'apply'
return that;
}
},
{
"brand": "B",
"brand_id": "253",
myParent: function() {
const that = this;
return that;
}
}
],
"discount": "20"
}]
}
function myFunction() {
let x = document.getElementById("myNumber").value;
let text = myVar.prods[0].grocery.find(el => el.brand_id === x).myParent().brand;
document.getElementById("demo").innerHTML = text;
}
<input type="number" id="myNumber" value="253">
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>