1

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"
}
1
  • What do you mean by "Standard notation is not working"? Please add a minimal reproducible example Commented Apr 25, 2022 at 11:43

1 Answer 1

1

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. `

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

9 Comments

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);
@Nash From Getting syntax error for this line : const res = obj.data.find(({ date }) => date == search);, I have a question. In your situation, you are enabling V8 runtime? I'm worried about this. If you are not enabling V8 runtime, please enable it and test it again. Ref
@Nash If you cannot use V8 runtime, how about the additional sample script? But, if I misunderstood your actual goal, I apologize.
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/…
Hi @Nash the code from this answer is perfectly appropriate for Apps Script. If it's not working for you (1) make sure you've enabled V8, (2) explain what error you are getting, (3) provide a minimal reproducible example.
|

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.