0

I am trying to flatten the "policy_metrics" array in the following JSON

{
"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",
                "stage": "achieved",
                "metric": "next_reply_time",
                "hours": -4
            }   
        ]   
    },  
    "allow_channelback": false,
    "allow_attachments": true
    }   
}

When I use

$.ticket.slas.policy_metrics[*]

I get the following which is what I want.

  • breach_at
  • stage
  • metric
  • days
  • hours

What I am not able to figure out is how to get the ticket attributes and the policy_metrics attributes. For example:

  • url
  • id
  • description
  • priority
  • status
  • breach_at
  • stage
  • metric
  • days
  • hours

If its not possible with JSONPath, example with Javascript will also work.

Thanks for your help.

Jerry
  • 31
  • 1
  • 7

1 Answers1

0

So I was able to make with Javascript

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",
                    "stage": "achieved",
                    "metric": "next_reply_time",
                    "hours": -4
                }   
            ]   
    },  
    "allow_channelback": false,
    "allow_attachments": true
    }   
}

console.log(flatten(obj));

The end result

{
  '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
}
Jerry
  • 31
  • 1
  • 7