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": "john@john.com",
    "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": "xxxxx@xxxxxx.com",
        "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 = xxxxx@xxxxxx.com                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....

Luke Toss
  • 356
  • 5
  • 23

2 Answers2

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?

Asis Patra
  • 69
  • 1
  • 9
1

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

var json = {
  "user": {
    "name": "John Smith",
    "email": "john@john.com",
    "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": "xxxxx@xxxxxx.com",
        "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);
henrique romao
  • 560
  • 1
  • 8
  • 32