0

I am trying to read JSON response data from a Restful API that contains a space in the path. I am trying to accomplish this in JavaScript, but can't seem to get the data to display.

Below is the JSON I am working with:

"fields": {
  "census": {...},
  "acs": {
    "meta": {
      "source": "American Community Survey from the US Census Bureau",
      "survey_years": "2014-2018",
      "survey_duration_years": "5"
    },
    "demographics": {
      "Median age": {
        "meta": {
          "table_id": "B01002",
          "universe": "Total population"
        },
        "Total": {
          "value": 32.8,
          "margin_of_error": 0.6

As you can see, the Median Age field has a space. The actual data I am trying to obtain is the Median age total value.

Below is my JSON path that isn't working:

        let ageDems = await response.data.results[0].fields.demographics.$["Median age"].Total.value;

I have also tried to use ['Median Age'], ["Median Age"] and Median+Age and none of those worked either. Can anyone please assist me in writing the correct path to obtain the data I need?

3
  • Give me a somewhat complete view of the data and I'll show you, but you'll need to provide a starting point. All I need is the acs part. Commented Feb 6, 2021 at 5:32
  • const ageDems = response.data.results[0].fields.demographics["Median age"].Total.value;, console.log(ageDems ) Commented Feb 6, 2021 at 6:23
  • If after reviewing my answer, you discover the error in your code, please upvote and accept the answer from Ismail who first answered your question with the same answer. Commented Feb 7, 2021 at 15:00

3 Answers 3

2
await response.data.results[0].fields.demographics["Median age"].Total.value;
Sign up to request clarification or add additional context in comments.

Comments

1

Ismail's suggestion does work.

This answer is here just to prove it to you.

const data = {
  "acs": {
    "meta": {
      "source": "American Community Survey from the US Census Bureau",
      "survey_years": "2014-2018",
      "survey_duration_years": "5"
    },
    "demographics": {
      "Median age": {
        "meta": {
          "table_id": "B01002",
          "universe": "Total population"
        },
        "Total": {
          "value": 32.8,
          "margin_of_error": 0.6
        }
      }
    }
  }
};

console.log(data.acs.demographics["Median age"].Total.value);

Comments

0

demographics is nested under acs:

await response.data.results[0].fields.acs.demographics["Median age"].Total.value

Comments

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.