Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

JMWE - How can I extract some string in specific line from another string?

Mateusz February 27, 2021

Hello :)

I'm using JMWE to call to Jira REST API and dump the response. Is there any possibility with use a Nunjucks syntax in JMWE to extract some line of dumped response? I want to set that line (not exactly the entire line, but some part of that) as a variable and comment issue.
Greetings.

2 answers

3 votes
David Fischer
Community Champion
February 27, 2021

If it's JSON, then you can set a variable to the value, and then access the whole structure like a regular JavaScript object:

{% set result = "HTTP://rest.api" | callJira(...) %}
{{ result.value }}

Now, if one of the properties of the returned value is a string and you want to extract part of it, you'll need to use a regular expression. For example:

{% set result = "HTTP://rest.api" | callJira(...) %}
{{ result.someString.match(r/^(\w).*/)[1] }}

 returns the first word of result.someString.

Mateusz February 28, 2021

I have more complex problem (that's only my opinion ;) ).

I've dumped JSON content from API response and I want to extract and set as variable part of that JSON which is on the end of that dumped response. I want to extract that part - "https://xyz.atlassian.net/servicedesk/customershim/secure/attachment/103871/abc.pdf?fromIssue=106038"

"content": "https://xyz.atlassian.net/servicedesk/customershim/secure/attachment/103871/abc.pdf?fromIssue=106038"
      }
    }
  ]
}

 I see that you use .match method from the JavaScript. JMWE let use not only Nunjucks syntax?

Thank you very much David for your time and help :)

David Fischer
Community Champion
February 28, 2021

Hi @Mateusz ,

as I mentioned, you should not _dump_ the value but just access the field inside the value, which is a JAvaScript object.

When you "dump" the result, what you get is a JSON representation of the object.

Can you post the whole result, as well as your code so far?

Like Mateusz likes this
Mateusz March 1, 2021

Hello again @David Fischer 

I've made a request to Jira Service Management Cloud API for Get attachments for request -> https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-request/#api-rest-servicedeskapi-request-issueidorkey-approval-approvalid-post 

and response is as following (Atlassian example):

{ "_expands": [], "size": 2, "start": 2, "limit": 2, "isLastPage": false, "_links": { "base": "https://your-domain.atlassian.net/rest/servicedeskapi", "context": "context", "next": "https://your-domain.atlassian.net/rest/servicedeskapi/request/IT-15/comment/1001/attachment?start=4&limit=2", "prev": "https://your-domain.atlassian.net/rest/servicedeskapi/request/IT-15/comment/1001/attachment?start=0&limit=2" }, "values": [ { "filename": "screenshot.png", "author": { "accountId": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "name": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "key": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "emailAddress": "fred@example.com", "displayName": "Fred F. User", "active": true, "timeZone": "Australia/Sydney", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "avatarUrls": { "48x48": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue", "24x24": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue", "16x16": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue", "32x32": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue" }, "self": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b" } }, "created": { "iso8601": "2015-10-09T10:22:00+0700", "jira": "2015-10-09T10:22:00.000+0700", "friendly": "Today 10:22 AM", "epochMillis": 1444360920000 }, "size": 23123, "mimeType": "image/png", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/attachment/10000", "content": "https://your-domain.atlassian.net/servicedesk/customershim/secure/attachment/10000/screenshot.png", "thumbnail": "https://your-domain.atlassian.net/servicedesk/customershim/secure/thumbnail/10000/_thumb_10000.png" } }, { "filename": "log.txt", "author": { "accountId": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "name": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "key": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "emailAddress": "fred@example.com", "displayName": "Fred F. User", "active": true, "timeZone": "Australia/Sydney", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "avatarUrls": { "48x48": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue", "24x24": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue", "16x16": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue", "32x32": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue" }, "self": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b" } }, "created": { "iso8601": "2015-10-09T10:22:00+0700", "jira": "2015-10-09T10:22:00.000+0700", "friendly": "Today 10:22 AM", "epochMillis": 1444360920000 }, "size": 32132, "mimeType": "text/plain", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/attachment/10001", "content": "https://your-domain.atlassian.net/servicedesk/customershim/secure/attachment/10001/log.txt" } } ] }

 

I want to set a variable as extract from above response only with part of content value at the end of response.

Mateusz March 1, 2021

Hello again @David Fischer 

I've made a request to Jira Service Management Cloud API for Get attachments for request -> https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-request/#api-rest-servicedeskapi-request-issueidorkey-attachment-get

JSON response is as following (Atlassian example):

{ "_expands": [], "size": 2, "start": 2, "limit": 2, "isLastPage": false, "_links": { "base": "https://your-domain.atlassian.net/rest/servicedeskapi", "context": "context", "next": "https://your-domain.atlassian.net/rest/servicedeskapi/request/IT-15/comment/1001/attachment?start=4&limit=2", "prev": "https://your-domain.atlassian.net/rest/servicedeskapi/request/IT-15/comment/1001/attachment?start=0&limit=2" }, "values": [ { "filename": "screenshot.png", "author": { "accountId": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "name": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "key": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "emailAddress": "fred@example.com", "displayName": "Fred F. User", "active": true, "timeZone": "Australia/Sydney", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "avatarUrls": { "48x48": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue", "24x24": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue", "16x16": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue", "32x32": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue" }, "self": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b" } }, "created": { "iso8601": "2015-10-09T10:22:00+0700", "jira": "2015-10-09T10:22:00.000+0700", "friendly": "Today 10:22 AM", "epochMillis": 1444360920000 }, "size": 23123, "mimeType": "image/png", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/attachment/10000", "content": "https://your-domain.atlassian.net/servicedesk/customershim/secure/attachment/10000/screenshot.png", "thumbnail": "https://your-domain.atlassian.net/servicedesk/customershim/secure/thumbnail/10000/_thumb_10000.png" } }, { "filename": "log.txt", "author": { "accountId": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "name": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "key": "qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "emailAddress": "fred@example.com", "displayName": "Fred F. User", "active": true, "timeZone": "Australia/Sydney", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b", "avatarUrls": { "48x48": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue", "24x24": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue", "16x16": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue", "32x32": "https://avatar-cdn.atlassian.com/9bc3b5bcb0db050c6d7660b28a5b86c9?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F9bc3b5bcb0db050c6d7660b28a5b86c9%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue" }, "self": "https://your-domain.atlassian.net/rest/api/2/user?username=qm:a713c8ea-1075-4e30-9d96-891a7d181739:5ad6d3581db05e2a66fa80b" } }, "created": { "iso8601": "2015-10-09T10:22:00+0700", "jira": "2015-10-09T10:22:00.000+0700", "friendly": "Today 10:22 AM", "epochMillis": 1444360920000 }, "size": 32132, "mimeType": "text/plain", "_links": { "jiraRest": "https://your-domain.atlassian.net/rest/api/2/attachment/10001", "content": "https://your-domain.atlassian.net/servicedesk/customershim/secure/attachment/10001/log.txt" } } ] }

 

How can I set as variable and extract from above object the value of field links and attribute content on the end of that object?

Mateusz March 1, 2021

Hello again @David Fischer 

I've made a request to Jira Service Management Cloud API for Get attachments for request -> https://developer.atlassian.com/cloud/jira/service-desk/rest/api-group-request/#api-rest-servicedeskapi-request-issueidorkey-attachment-get

JSON response is as following (Atlassian example):

Screenshot_2021-03-01 The Jira Service Management Cloud REST API.png

(I couldn't paste .txt version)

How can I set as variable and extract from above object the value of field links and attribute content from the end of that object?

David Fischer
Community Champion
March 1, 2021

So, when you call the Jira REST API using callJira, the result is actually that JavaScript structure you posted. Therefore, you can manipulate it like a JavaScript object.

In your case, the only problem is: what do you want to do if there's more than one attachment? As you can see, the response is an array of objects, one per attachment...

Assuming you only want the first attachment, you could do something like:

{% set result = "HTTP://rest.api" | callJira(...) %}
{{ result.values[0]._links.content }}
Like Mateusz likes this
Mateusz March 21, 2021

Thank you very much David, your message gave me a wider perspective on that and all "problems" in scripts :)

BTW - I have some migration to do with one-attachment-issues (for my luck :) )

0 votes
Mateusz February 27, 2021

PS. I didn't find any similar function for that in Nunjucks documentation.

David Fischer
Community Champion
February 27, 2021

What does the returned value look like? Is it just plain text or JSON?

Like Mateusz likes this
Mateusz February 27, 2021

It's JSON but I want to get answers for both of them :)

Suggest an answer

Log in or Sign up to answer