How to access a given object within JSON via Axios

Asked

Viewed 95 times

1

I want to get the data of product_id, product_name and product_price using Axios.

{
    "status": "success",
    "url": {
        "https://api.exemplo.com/product-1": {
            "data": {
                "product_id": "1",
                "product_name": "bola",
                "product_price": 30
            }
        }
    }
}
  • It would be better if you put the code where you use Aces and describe the problem more, so you can better understand your question.

1 answer

2


In case, apparently, you want to get the data contained in JSON within the JavaScript. For this it is enough to unstructure the object:

const resposta = {
  "status": "success",
  "url": {
    "https://api.exemplo.com/product-1": {
      "data": {
        "product_id": "1",
        "product_name": "bola",
        "product_price": 30
      }
    }
  }
}


const { url } = resposta;
const [ [_, conteudo] ] = Object.entries(url);
const {
  data: {
    product_id,
    product_name,
    product_price,
  }
} = conteudo;

console.log(product_id, product_name, product_price);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.