-3

I've the following data in my local storage:

[{
  "event": "addToCart",
  "eventLabel": "Test Product",
  "ecommerce": {
    "currencyCode": "USD",
    "add": {
      "products": [{
        "name": "Test Product",
        "id": "78789",
        "price": "429.00",
        "category": "Catalog Page",
        "list": "Massage\/Massage Chairs\/Portable Massage Chairs",
        "quantity": "1"
      }]
    }
  }
}]

and I need to access to the value of ecommerce. So obtaining the following data:

"ecommerce": {
  "currencyCode": "USD",
  "add": {
    "products": [{
      "name": "Test Product",
      "id": "78789",
      "price": "429.00",
      "category": "Catalog Page",
      "list": "Massage\/Massage Chairs\/Portable Massage Chairs",
      "quantity": "1"
    }]
  }
}

How can I do it?

Ramesh KR
  • 143
  • 1
  • 9
  • Does this answer your question? [how to retrieve data from local storage?](https://stackoverflow.com/questions/42219531/how-to-retrieve-data-from-local-storage) – Francesco Lisandro Apr 27 '21 at 18:57
  • No I'm able to get local storage but I don't know how to create array as I mentioned above – Ramesh KR Apr 27 '21 at 18:58

1 Answers1

0

First you have to parse the json you have

var data = JSON.parse(json);

then you can access any data key and values like this:

var ecom = data.ecommerce 
Omar
  • 1
  • 1