admin管理员组文章数量:1295257
I'm working with some JSON data and trying to transform it using JMESPath, but I'm running into some issues with the way the data is being mapped.
I have the following input data:
{
"results": [
{"page_name": "home", "url": ";, "time": 1625760290, "response_time": 120},
{"page_name": "about", "url": ";, "time": 1625760390, "response_time": 150},
{"page_name": "home", "url": ";, "time": 1625760490, "response_time": 100}
],
"test_name": "Test1"
}
And I'm trying to create a transformation like this:
{
"results": [
{
"metricId": "Response Time",
"unit": "ms",
"data": [
{
"sourcesMap": {
"PageName": "home",
"PageURL": ";
},
"timestamps": [1625760290, 1625760490],
"values": [120, 100]
},
{
"sourcesMap": {
"PageName": "about",
"PageURL": ";
},
"timestamps": [1625760390],
"values": [150]
}
]
}
],
"test_name": "Test1"
}
I've tried the following JMESPath query to extract the data, but it doesn't give me the desired result:
{
"results": [
{
"metricId": "Response Time",
"unit": "ms",
"data": "$results[*].page_name | [].{ \
sourcesMap: { \
PageName: @, \
PageURL: results[?page_name==@].url \
}, \
timestamps: results[?page_name==@].time, \
values: results[?page_name==@].response_time \
}",
}
],
"test_name": "$test_name",
}"
However, the result I get looks like this:
{
"results": [
{
"metricId": "Response Time",
"unit": "ms",
"data": [
{
"sourcesMap": {
"PageName": "home",
"PageURL": null
},
"timestamps": null,
"values": null
}
]
}
]
}
In this expression above, I apply jmespath.search(query, self.data_json)
on each expression starting with a $
It seems like the JMESPath query isn't properly handling the nested values, and it's returning null instead of the actual timestamps and values arrays.
Is it possible to achieve this nested transformation with JMESPath, or do I need to handle this differently?
I'm working with some JSON data and trying to transform it using JMESPath, but I'm running into some issues with the way the data is being mapped.
I have the following input data:
{
"results": [
{"page_name": "home", "url": "http://home", "time": 1625760290, "response_time": 120},
{"page_name": "about", "url": "http://about", "time": 1625760390, "response_time": 150},
{"page_name": "home", "url": "http://home", "time": 1625760490, "response_time": 100}
],
"test_name": "Test1"
}
And I'm trying to create a transformation like this:
{
"results": [
{
"metricId": "Response Time",
"unit": "ms",
"data": [
{
"sourcesMap": {
"PageName": "home",
"PageURL": "http://home"
},
"timestamps": [1625760290, 1625760490],
"values": [120, 100]
},
{
"sourcesMap": {
"PageName": "about",
"PageURL": "http://about"
},
"timestamps": [1625760390],
"values": [150]
}
]
}
],
"test_name": "Test1"
}
I've tried the following JMESPath query to extract the data, but it doesn't give me the desired result:
{
"results": [
{
"metricId": "Response Time",
"unit": "ms",
"data": "$results[*].page_name | [].{ \
sourcesMap: { \
PageName: @, \
PageURL: results[?page_name==@].url \
}, \
timestamps: results[?page_name==@].time, \
values: results[?page_name==@].response_time \
}",
}
],
"test_name": "$test_name",
}"
However, the result I get looks like this:
{
"results": [
{
"metricId": "Response Time",
"unit": "ms",
"data": [
{
"sourcesMap": {
"PageName": "home",
"PageURL": null
},
"timestamps": null,
"values": null
}
]
}
]
}
In this expression above, I apply jmespath.search(query, self.data_json)
on each expression starting with a $
It seems like the JMESPath query isn't properly handling the nested values, and it's returning null instead of the actual timestamps and values arrays.
Is it possible to achieve this nested transformation with JMESPath, or do I need to handle this differently?
Share Improve this question edited Feb 13 at 21:11 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Feb 12 at 9:35 AnayaAnaya 33 bronze badges 2- This question is similar to: How to get list of values of same key from a JSON using JMESPATH. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – β.εηοιτ.βε Commented Feb 13 at 19:11
- So you want to agregate your data by url ? – bosskay972 Commented Feb 18 at 10:48
1 Answer
Reset to default 0Here's the solution for your example :
{results:[{metricId:'Response Time',unit:'ms',data:[{sourceMap : {PageName: 'home', PageURL : @.results[?page_name == 'home'].url | [0]},timestamp: @.results[?page_name == 'home'].time,values : @.results[?page_name == 'home'].response_time},{sourceMap : {PageName: 'about', PageURL : @.results[?page_name == 'about'].url | [0]},timestamp: @.results[?page_name == 'about'].time,values : @.results[?page_name == 'about'].response_time}]}],test_name:@.test_name}
But as you can see I do it in an hardcoded way by putting "home" and "about" directly in the query. Unfortunatly JMESPATH would not allow you to do a transformation like this without an hardcoded query. You can use jq to do this.
本文标签: Is it possible to achieve a nested object transformation with JMESPathStack Overflow
版权声明:本文标题:Is it possible to achieve a nested object transformation with JMESPath? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741611282a2388274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论