1

I am having very basic question: but I am unable to find how to fetch a value from this

{
    "content": {
        "laptops": {
            "brands": {
                "company": "xyz",
                "specification": {
                    "name": {
                        "description": "xyzName",
                        "type": "string"
                    },
                    "status": {
                        "description": "abcName",
                        "type": "string"
                    }
                }
            }
        }
    }
}

I want to fetch the brand value from the above response: ServiceClass:

getSample(): Promise<> {
    return http
      .get("http://localhost:8080/user")
      .then((response) => {
        console.log("sample",response);
        return response.data;
      })
      .catch((error) => {
        throw error;
      });
  }
}

useEffect:

 useEffect(() => {
      service
      .getSample()
      .then((data) => {
        console.log("response", data);
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);

In console I am getting the complete response but I only want to fetch the "brands" Can any one help me with this!

Singh
  • 207
  • 1
  • 5
  • 14
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – gbalduzzi Jul 31 '20 at 14:21
  • 2
    You should be able to dig into your data object to get that. Something like `data.content.laptops.brands`. – justDan Jul 31 '20 at 14:22
  • Thats the first thing I tried to write data.content, it gives an error: Property does not exist – Singh Jul 31 '20 at 14:25
  • 3
    If `response.data` is a string you will need to parse it to a JavaScript object with `JSON.parse`. So, `const content = JSON.parse(data);` and then you can get the brands from `content.laptop.brands`. – mekwall Jul 31 '20 at 14:26

0 Answers0