Home > Back-end >  How to access a specific element in JSON in Google Apps Script?
How to access a specific element in JSON in Google Apps Script?

Time:04-26

I need to access the nav for a specific date from the below JSON. eg : data[date="20-04-2022"].nav How do I do it in Google Apps Script? The standard JSON notation is not working.

{
   "meta":{
      "fund_house":"Mutual Fund",
      "scheme_type":"Open Ended Schemes",
   },
   "data":[
      {
         "date":"22-04-2022",
         "nav":"21.64000"
      },
      {
         "date":"21-04-2022",
         "nav":"21.69000"
      },
      {
         "date":"20-04-2022",
         "nav":"21.53000"
      }
   ],
   "status":"SUCCESS"
}

CodePudding user response:

In your situation, I thought that it is required to retrieve the element including "date": "20-04-2022" from the array of data. So, how about the following sample script?

Sample script:

const obj = {
  "meta": {
    "fund_house": "Mutual Fund",
    "scheme_type": "Open Ended Schemes",
  },
  "data": [
    {
      "date": "22-04-2022",
      "nav": "21.64000"
    },
    {
      "date": "21-04-2022",
      "nav": "21.69000"
    },
    {
      "date": "20-04-2022",
      "nav": "21.53000"
    }
  ],
  "status": "SUCCESS"
};
const search = "20-04-2022";
const res = obj.data.find(({ date }) => date == search);
const value = res && res.nav;
console.log(value) // 21.53000

  • For example, if the search value is always found, you can use the following script.

      const res2 = obj.data.find(({ date }) => date == search).nav;
    

Reference:

Added 1:

From your following reply,

This looks like standard java script. Does not work in google apps script(script.google.com/home). Getting syntax error for this line : const res = obj.data.find(({ date }) => date == search);

I'm worried that you are not enabling V8 runtime. Ref If you cannot use V8 runtime, how about the following sample script?

Sample script:

var obj = {
  "meta": {
    "fund_house": "Mutual Fund",
    "scheme_type": "Open Ended Schemes",
  },
  "data": [
    {
      "date": "22-04-2022",
      "nav": "21.64000"
    },
    {
      "date": "21-04-2022",
      "nav": "21.69000"
    },
    {
      "date": "20-04-2022",
      "nav": "21.53000"
    }
  ],
  "status": "SUCCESS"
};
var search = "20-04-2022";
var res = obj.data.filter(function (e) { return e.date == search })[0];
var value = res && res.nav;
console.log(value) // 21.53000

Added 2:

From your following reply,

This looks like standard java script. Does not work in google apps script(script.google.com/home). Getting syntax error for this line : const res = obj.data.find(({ date }) => date == search);

I am trying to write a google apps script to fetch data from a url. But google seems to have its own way of handling the JSON data which I am unable to figure out. developers.google.com/apps-script/guides/services/…

I understood that your actual goal was to retrieve the value using Web Apps. If my understanding of your actual goal, how about the following sample script?

1. Sample script:

Please copy and paste the following script to the script editor and save the script.

function doGet(e) {
  var search = e.parameter.search;
  var obj = {
    "meta": {
      "fund_house": "Mutual Fund",
      "scheme_type": "Open Ended Schemes",
    },
    "data": [
      {
        "date": "22-04-2022",
        "nav": "21.64000"
      },
      {
        "date": "21-04-2022",
        "nav": "21.69000"
      },
      {
        "date": "20-04-2022",
        "nav": "21.53000"
      }
    ],
    "status": "SUCCESS"
  };
  var res = obj.data.filter(function (e) { return e.date == search })[0];
  var value = res && res.nav;
  return ContentService.createTextOutput(value);
}
  • I think that this sample script can be used with and without V8 runtime.

2. Deploy Web Apps.

In this case, please check the official document. Please set it as follows.

  • Execute as: Me
  • Anyone with Google account: Anyone

In this case, it supposes that you are using new IDE. Please be careful this.

3. Testing.

Please access to the URL like https://script.google.com/macros/s/{deploymentId}/exec?search=20-04-2022 using your browser. By this, the result value is returned. `

  • Related