Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
API data won't convert to JSON format

Had the data in JSON in a previous build of my app but (with the help of StackOverflow community) redesigned how I call my API to fix various other breaking errors.

This is what I just tried. I log both the "regular" output and the attempt at converting it to JSON. The non-json output comes up as "{data: Array}" in my console while the JSON is undefined.

  componentDidMount() {
    axios
      .get(
        "API_KEY"
      )
      .then((res) => {
        const data = res.data
        const json = data.json
        this.setState({ data, loading: false });
        console.log(json);
        console.log(data)
      });
  }

And here is my other attempt:

  componentDidMount() {
    axios.get("API_KEY").then((res) => {
      const data = res.data;
      this.setState({ data, loading: false });
      console.log(data);
      console.log(JSON.parse(JSON.stringify(data)));
      
    });
  }

All your help and advice is greatly appreciated! :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would suggest a better syntax using ES6.

  const componentDidMount = async () => {
    let { data } = await axios.get("API_KEY")
    
    // Your data by default should be object or array.
    // Regardless, both should worked fine.
    console.log(data)
    
    // In case it is just string and not undefined
    if (data && typeof data !== 'object') {
        data = JSON.parse(data)
    }

    this.setState({ data, loading: false })
  }

Otherwise, try using breakpoint or console.log in the first place after getting the result. If the result is undefined, perhaps the promise is yet to resolve.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!