0

I have a CURL request that will follow a standard request/response like the below.

Request

curl -X GET --header 'Accept: application/vnd.pager+json;version=2' --header 'Authorization: Token token=xxxxxxxxxxxxx' 'https://api.pager.com/users/12345?include%5B%5D=contact_methods'

Response

{
  "user": {
    "name": "John Smith",
    "email": "[email protected]",
    "time_zone": "America/Los_Angeles",
    "color": "indigo",
    "avatar_url": "xxxx",
    "billed": true,
    "role": "user",
    "description": null,
    "invitation_sent": false,
    "contact_methods": [
      {
        "id": "TP3D6TTZ",
        "type": "email_contact_method",
        "summary": "Default",
        "self": "xxxxx",
        "html_url": null,
        "label": "Default",
        "address": "[email protected]",
        "send_short_email": false,
        "send_html_email": true
      },
      {
        "id": "TPK4ZULL",
        "type": "phone_contact_method",
        "summary": "Work",
        "self": "xxxxxxxxx",
        "html_url": null,
        "label": "Work",
        "address": "5557304169",
        "country_code": 1,
        "blacklisted": false
      }
    ],
}

What I want it to be able to strip some of the data out using Javascript and re-use them variables later on in my project.

So from the response I would like something like....

Var1 = John Smith                      Always the first 'name'
Var2 = email_contact_method            Always the first 'type'
Var3 = [email protected]                Always the first 'address'
Var4 = phone_contact_method            Always the second 'type'
Var5 = 5557304169                      Always the second 'address'

I have tried somethings but I just don't know how to go about selecting only the fields that I want. Any pointers would be great....

2 Answers 2

1

You need to access the values using the json keys.
It would be something like this:

var json = {
  "user": {
    "name": "John Smith",
    "email": "[email protected]",
    "time_zone": "America/Los_Angeles",
    "color": "indigo",
    "avatar_url": "xxxx",
    "billed": true,
    "role": "user",
    "description": null,
    "invitation_sent": false,
    "contact_methods": [
      {
        "id": "TP3D6TTZ",
        "type": "email_contact_method",
        "summary": "Default",
        "self": "xxxxx",
        "html_url": null,
        "label": "Default",
        "address": "[email protected]",
        "send_short_email": false,
        "send_html_email": true
      },
      {
        "id": "TPK4ZULL",
        "type": "phone_contact_method",
        "summary": "Work",
        "self": "xxxxxxxxx",
        "html_url": null,
        "label": "Work",
        "address": "5557304169",
        "country_code": 1,
        "blacklisted": false
      }
    ]
}
};

var name = json.user.name;
var type1 = json.user.contact_methods[0].type;
var address1 = json.user.contact_methods[0].address;
var type2 = json.user.contact_methods[1].type;
var address2 = json.user.contact_methods[1].address;
console.log(name, type1, address1, type2, address2);

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, I was unsure on how to call the contact methods address, but this explains it perfectly. Thank you
Glad that this helped you! Happy coding.
1

You can use something like JSON.parse(json)

var json = '{"result":true,"count":1}',
    obj = JSON.parse(json);

alert(obj.count);

This link may help you. Parse JSON in JavaScript?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.