Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

get JSON data value from GET API with TOKEN

Edited

I'm trying to fetch an API return; result in JSON FORMAT. Using ScriptRunner JOB - will that be possible to have those results.

 

URL:  https://slack.com/api/users.lookupByEmail?token=<sometoken>&email=<value that I'll provide>

 

I would like to have this return value of a particular element. i.e. ID (bold below)

 

{"ok":true,"user":{"id":"U013MMEPMHC","team_id":"<someother>","name":"piyush.1502","deleted":false,"color":"e7392d","real_name":"piyush.live","tz":"Asia\/Kolkata","tz_label":"India Standard Time","tz_offset":19800}}

 I may need the whole sort of line code to figure this out. 

 

Type: JIRA SERVER

ScriptRunner - Yes

 

With Below: Able to fetch the data - but how to only extract the required field and save it in a variable.

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*


def http = new HTTPBuilder('https://slack.com/api/users.lookupByEmail?token=xxxxxxxxxxxxxxx&email=piyush.1502@live.com')
http.request(GET) { requestContentType = ContentType.JSON
response.success = { resp, JSON -> return JSON
}
response.failure = { resp ->
return "Request failed with status ${resp.status}"
}
}

1 answer

1 accepted

1 vote
Answer accepted

I've edited your question to remove the token from your code block... you probably want to keep that private.

But the response.success is already parsing the JSON for you into a nice object structure.

You could just add .user.id after the last curly brace, but something like this will be safer:

def responseObj

def http = new HTTPBuilder('https://slack.com/api/users.lookupByEmail?token=xxxxxxxxxxxxxxx&email=piyush.1502@live.com')
http.request(GET) {
requestContentType = ContentType.JSON
response.success = { resp, JSON ->
responseObj = JSON
}
response.failure = { resp ->
responseObj= [error: "Request failed with status ${resp.status}"]
}
}
if(!responseObj?.error){
userId = responseObj.user.id
}

Thanks @Peter-Dave Sheehan . For the token and the answer. 

I updated it as 

 

responseObj = JSON.user ? JSON.user.id : null

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

🎨 Add some visual life to your templates

Hi Atlassian Community, My name is Avni Barman, and I am a Product Manager on the Confluence Cloud team. Based on feedback from you, we are giving admins more power to create templates that a...

131 views 1 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you