Question

Expression builder, extracting json from ticket description

  • 5 June 2023
  • 4 replies
  • 464 views

Badge

I’m trying to extract json from the ticket description which is in the middle of text. I can use the split function to get the value however I cannot get the syntax correct. Can anyone help please?

 

 


4 replies

Userlevel 4
Badge +4

@SfrsDL 

In case “bbb” is the wanted result, you can use the following body (including leading and ending single quotes):

 

'{{ticket.description | split: "ITUSE---" | last | split:"---" | first }}'

 

 

Let me know if this is what you want to achieve.

 

Kind regards,

Rutger Bockholts

Freshworks

Badge

@SfrsDL

In case “bbb” is the wanted result, you can use the following body (including leading and ending single quotes):

 

'{{ticket.description | split: "ITUSE---" | last | split:"---" | first }}'

 

 

Let me know if this is what you want to achieve.

 

Kind regards,

Rutger Bockholts

Freshworks

Thanks for getting back to me, I’ve tried what you recommened but do not get the same result?

 

 

Badge +2

To extract JSON data from a ticket description using an expression builder, you can follow these steps:

1. Identify the JSON Data: Review the ticket description to determine where the JSON data is located. It might be enclosed within specific markers or have identifiable patterns.

2. Access the Ticket Description: In your expression builder or programming language, obtain the ticket description text. This can be done by retrieving the ticket object or accessing the relevant field containing the description.

3. Parse the JSON Data: Use a JSON parsing method or library provided by your programming language to extract the JSON data from the ticket description. The specific method or library will depend on the programming language you are using. Some common options include `json.loads()` in Python, `JSON.parse()` in JavaScript, or `JSONObject` in Java.

4. Extract the Desired Data: Once the JSON data is parsed, you can navigate the resulting JSON object or array to extract the specific information you need. This can be done by accessing the desired keys or indexes within the JSON structure.

5. Handle Errors: It's essential to handle any potential errors that may occur during the JSON parsing process. For example, if the ticket description does not contain valid JSON, you may encounter parsing exceptions. Implement appropriate error handling mechanisms to handle such situations gracefully.

Here's an example in Python using the `json` library:

```python
import json

# Assume `ticket` is the ticket object with a `description` field containing the JSON data

ticket_description = ticket.description

try:
    json_data = json.loads(ticket_description)
    # Extract the desired information from the parsed JSON data
    # For example, if there is a key named 'name' in the JSON object:
    name = json_data['name']
    print('Name:', name)
except json.JSONDecodeError:
    print('Invalid JSON format in ticket description')
```

Keep in mind that the specific implementation details may vary depending on your programming language, the structure of the JSON data, and the environment or framework you are working with. Adjust the code accordingly based on your requirements and the tools available to you.

Userlevel 4
Badge +4

@SfrsDL , weird indeed, I don't get the same result either.

For now you can use 2 Expression nodes to get the right result:

 

input: “aaa---ITUSE---bbb---ITUSE---ccc”

Expression node1:
substring('{{ticket.description}}',indexOf('{{ticket.description}}','ITUSE---', 0)+8,length('{{ticket.description}}'))

Result:
bbb---ITUSE---ccc

 

 

Expression node2 (with placeholder of result of the first expression node, “E2 might be different in your case):
'{{E2.result | split:"---" | first }}'

Result:
bbb

 

Regards,

 

Rutger Bockholts

Freshworks

Reply