问题描述
我试图在以下JSON中展平“ policy_metrics”数组
{
"ticket": {
"url": "https://company.zendesk.com/api/v2/tickets/12107.json","id": 12107,"description": "test","priority": "high","status": "open","slas": {
"policy_metrics": [
{
"breach_at": "2020-09-18T09:27:53Z","stage": "active","metric": "pausable_update_time","days": 2
},{
"breach_at": null,"stage": "achieved","metric": "first_reply_time"
},{
"breach_at": "2020-09-16T09:18:56Z","metric": "next_reply_time","hours": -4
}
]
},"allow_channelback": false,"allow_attachments": true
}
}
当我使用
$.ticket.slas.policy_metrics[*]
我得到以下是我想要的。
- breach_at
- 阶段
- 指标
- 天
- 小时
我无法弄清楚如何获取票证属性和policy_metrics属性。例如:
- 网址
- id
- 说明
- 优先
- 状态
- breach_at
- 阶段
- 指标
- 天
- 小时
如果JSONPath无法实现,则Javascript示例也将起作用。
感谢您的帮助。
解决方法
所以我能够用Java语言制作
const flatten = require('flat').flatten;
const obj = {
"ticket": {
"url": "https://company.zendesk.com/api/v2/tickets/12107.json","id": 12107,"description": "test","priority": "high","status": "open","slas": {
"policy_metrics": [
{
"breach_at": "2020-09-18T09:27:53Z","stage": "active","metric": "pausable_update_time","days": 2
},{
"breach_at": null,"stage": "achieved","metric": "first_reply_time"
},{
"breach_at": "2020-09-16T09:18:56Z","metric": "next_reply_time","hours": -4
}
]
},"allow_channelback": false,"allow_attachments": true
}
}
console.log(flatten(obj));
最终结果
{
'ticket.url': 'https://company.zendesk.com/api/v2/tickets/12107.json','ticket.id': 12107,'ticket.description': 'test','ticket.priority': 'high','ticket.status': 'open','ticket.slas.policy_metrics.0.breach_at': '2020-09-18T09:27:53Z','ticket.slas.policy_metrics.0.stage': 'active','ticket.slas.policy_metrics.0.metric': 'pausable_update_time','ticket.slas.policy_metrics.0.days': 2,'ticket.slas.policy_metrics.1.breach_at': null,'ticket.slas.policy_metrics.1.stage': 'achieved','ticket.slas.policy_metrics.1.metric': 'first_reply_time','ticket.slas.policy_metrics.2.breach_at': '2020-09-16T09:18:56Z','ticket.slas.policy_metrics.2.stage': 'achieved','ticket.slas.policy_metrics.2.metric': 'next_reply_time','ticket.slas.policy_metrics.2.hours': -4,'ticket.allow_channelback': false,'ticket.allow_attachments': true
}