0

When I try to parse json in google appscript using the following code, I was expecting dataAll as an object but the debugger shows dataAll as string and because of this reason, I couldn't parse the json.

var response = UrlFetchApp.fetch(aUrl, getUrlFetchOptions());
var resposnsestring = response.getContentText();
var dataAll = JSON.parse(response.getContentText()); 

Here is the data/

{
  "DataSet" : [ {
    "Record" : [ {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000GcJUfAAN"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000GcJUfAAN"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000GcJUDAA3"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000GcJUDAA3"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000GcJUEAA3"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000GcJUEAA3"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl0AAB"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000Lpbl0AAB"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl1AAB"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000Lpbl1AAB"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl2AAB"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000Lpbl2AAB"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl3AAB"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000Lpbl3AAB"
    }, {
      "attributes" : {
        "type" : "Opportunity",
        "url" : "/services/data/v31.0/sobjects/Opportunity/006i000000A89SCAAZ"
      },
      "Name" : "EM- Deadul Technology",
      "Amount" : 100.00,
      "AccountId" : "001i000000Q9c8vAAB",
      "Account" : {
        "attributes" : {
          "type" : "Account",
          "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
        },
        "Name" : "Ellie Mae",
        "Id" : "001i000000Q9c8vAAB"
      },
      "Id" : "006i000000A89SCAAZ"
    } ],
    "ObjectName" : "Opportunity",
    "DataSetName" : "opp"
  }, {
    "Record" : [ {
      "attributes" : {
        "type" : "Account",
        "url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
      },
      "Name" : "Ellie Mae",
      "Phone" : "(671) 214-5678",
      "Fax" : "(671) 214-5600",
      "Id" : "001i000000Q9c8vAAB"
    } ],
    "ObjectName" : "Account",
    "DataSetName" : "acc"
  } ]
}
1
  • 1
    Use Logger.log(responsestring); to check if the data is correct. Maybe also use jsonlint.com on that string to verify. Commented Sep 17, 2014 at 23:24

2 Answers 2

1

Use this:

var dataAll = JSON.parse(resposnsestring);

If JSON.parse() doesn't return an object, then there is something wrong with how the string is constructed, it's not in JSON format.

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

2 Comments

I tried and same result. If I copy the string into online json editor, it shows the output in object form and this means the json data is correct. I also use the same output in a .net program and it works fine.
Refeekh, did you ever solve this? I am having a similar problem.
1

The json data I received from the source had escape characters and when I called json.parse, it return a string without the escape char's. I used json.parse again on the new string which returned an object

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.