简体   繁体   中英

Parse a JSON Array from a GET API Call in google apps script

I use a GET API call to an endpoint and get the output as follows:

  [
    {
        "available": false,
        "occasional": false,
        "id": 36005656995,
        "signature": "<div dir=\"ltr\"><p><br></p>\n</div>",
        "ticket_scope": 1,
        "created_at": "2018-06-20T10:13:25Z",
        "updated_at": "2018-08-29T06:37:21Z",
        "available_since": null,
        "contact": {
            "active": false,
            "email": "abcd@clasher.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-08-15T05:50:27Z",
            "mobile": null,
            "name": "abcd@clasher.com",
            "phone": "00903030333",
            "time_zone": "Chennai",
            "created_at": "2018-03-19T01:56:53Z",
            "updated_at": "2018-09-03T04:41:59Z"
        }
    },
    {
        "available": false,
        "occasional": true,
        "id": 36004999676,
        "signature": "<div dir=\"ltr\"><p><br></p>\n</div>",
        "ticket_scope": 1,
        "created_at": "2018-03-07T05:47:43Z",
        "updated_at": "2018-06-21T12:45:41Z",
        "available_since": null,
        "contact": {
            "active": true,
            "email": "sab@clashuniversal.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-03-07T05:49:16Z",
            "mobile": null,
            "name": "Sample Agent",
            "phone": null,
            "time_zone": "Chennai",
            "created_at": "2018-03-07T05:47:43Z",
            "updated_at": "2018-05-28T10:39:39Z"
        }
    },
    {
        "available": false,
        "occasional": false,
        "id": 36004979341,
        "signature": "<div dir=\"ltr\"><p>Regards<br>Clashuniversal</p></div>",
        "ticket_scope": 1,
        "created_at": "2018-03-06T15:27:59Z",
        "updated_at": "2018-10-02T19:51:12Z",
        "available_since": "2018-08-10T14:10:09Z",
        "contact": {
            "active": true,
            "email": "prasadclasher@gmail.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-09-30T06:02:21Z",
            "mobile": null,
            "name": "Subramania Prasad",
            "phone": "9999999998",
            "time_zone": "Chennai",
            "created_at": "2018-03-06T15:27:59Z",
            "updated_at": "2018-09-20T08:43:18Z"
        }
    }
]

Note that the response as such is a JSON Array.

What I want to do is to parse this into a Javascript object so that I can do a bit of processing on this data.

For parsing this data I am using the following code:

test_response = UrlFetchApp.fetch(domain,obj);
test_resp_string = test_response.getContentText();
object_1 = JSON.parse(test_resp_string);

The expectation of above code is that I would like to access each of the object of the response separately for example when I do Logger.log(object_1[0]) should give me the first element of the returned response as below:

{
        "available": false,
        "occasional": false,
        "id": 36005656995,
        "signature": "<div dir=\"ltr\"><p><br></p>\n</div>",
        "ticket_scope": 1,
        "created_at": "2018-06-20T10:13:25Z",
        "updated_at": "2018-08-29T06:37:21Z",
        "available_since": null,
        "contact": {
            "active": false,
            "email": "abcd@clasher.com",
            "job_title": null,
            "language": "en",
            "last_login_at": "2018-08-15T05:50:27Z",
            "mobile": null,
            "name": "abcd@clasher.com",
            "phone": "00903030333",
            "time_zone": "Chennai",
            "created_at": "2018-03-19T01:56:53Z",
            "updated_at": "2018-09-03T04:41:59Z"
        }
    }

However when I do Logger.log(object_1[0]), I get the following response:

 {
  updated_at=2018-08-29T06:37:21Z, 
  signature=<div dir="ltr"><p><br></p></div>, 
  ticket_scope=1, 
  contact={
  last_login_at=2018-08-15T05:50:27Z, 
  updated_at=2018-09-03T04:41:59Z, 
  phone=00903030333, 
  mobile=null, 
  name=abcd@clasher.com, 
  active=false, 
  created_at=2018-03-19T01:56:53Z, 
  language=en, 
  time_zone=Chennai, 
  job_title=null, 
  email=abcd@clasher.com}, 
  available=false, 
  created_at=2018-06-20T10:13:25Z, 
  occasional=false, 
  id=3.6005656995E10, 
  available_since=null
}

You'd be able to notice the following differences between what is expected and what I get:

  1. In the response that I get the order of the output is varied, I'm curious to know why it is so,For-ex. In the response of original call the first key is "available" while after parsing the data it is "updated_at"

  2. The id key has a value say 36005656995 in the original response, however after converting it into a javascript object using JSON.parse the id value is 3.6005656995E10 . Because of this conversion, I'm not able to do the post processing. I also could not find a way to explicitly convert it into a string(so that the number is preserved exactly as such in the original response)while parsing JSON .

I was able to use to.String() to convert the number to the original ID seen in the response, however I'm still curious to find out the answer for the following query:

How do I preserve the value of ID from the original response without converting it into float or long

I hope I was clear with my query, thanks in advance for going through the long query.

When you use JSON.parse() , the payload string is converted to an array of objects. The order of the items in the top-level array will be retained. However each array item is an object and the order of the properties on those objects is not guaranteed, nor does it need to be since you'll be referencing those properties by name (using either dot notation or bracketed lookup).

The id key is fine, its just being represented in scientific notation which is standard for large numbers. If you need the number in pure decimal format as a string use the toFixed() method to convert it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM