blac040
blac040

Reputation: 127

How to access JSON?

I am wrote API method, after calling that method , I got my response like

[
    {
        "spark_version": "7.6.x-scala2.12"
    }
]

Now I want to have variable in my API method which store value 7.6.x-scala2.12.

API Controller method

 [HttpGet]
        public IActionResult GetTest(int ActivityId)
        {
            string StoredJson = "exec sp_GetJobJSONTest " +
                                "@ActivityId = " + ActivityId ;
            var result =  _context.Test.FromSqlRaw(StoredJson);
            return Ok(result);
        }

So how this variable should call on this response to get string stored in spark_version?

Thank you

Upvotes: 0

Views: 58

Answers (1)

Tom
Tom

Reputation: 1188

As you have the JavaScript tag, here's how you'd do it in JS:

If you are able to call your API method, then you can just assign the response to a variable. For example, if you are calling it using the fetch API, it would look something like:

let apiResponse;

fetch('myApiUrl.com').then((response) => {
  if (response.status === 200) {
    apiResponse = response.body;
    console.log('Response:', apiResponse[0]['spark_version']);
  }
});

(I defined the variable outside the then to make it globally accessible)

Upvotes: 1

Related Questions