0

I have trouble parsing a JSON object,

this is my code

var k = '[{"image:loc":["https://cdn.shopify.com/s/files/1/0094/2252/products/YZY-KW3027.053.jpg?v=1539344090"],"image:title":["Yeezy WMNS Tubular Boot Washed Canvas - Limestone"]}]'
var kP = JSON.parse(k);

console.log(kP);

But when I do try to parse "image:loc" or "image:title" like this: console.log(kP['image:loc']); it returns undefined.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bob Jensen
  • 37
  • 3
  • 8

2 Answers2

0

Since kP is an array, to access any property from that you have to use proper index:

var k = '[{"image:loc":["https://cdn.shopify.com/s/files/1/0094/2252/products/YZY-KW3027.053.jpg?v=1539344090"],"image:title":["Yeezy WMNS Tubular Boot Washed Canvas - Limestone"]}]'
var kP = JSON.parse(k);

console.log(kP);
console.log(kP[0]['image:loc']);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

console.log(kP['image:loc']); doesn't work as kP is an array. You need to target the first index of the array to target your object like so:

var k = '[{"image:loc":["https://cdn.shopify.com/s/files/1/0094/2252/products/YZY-KW3027.053.jpg?v=1539344090"],"image:title":["Yeezy WMNS Tubular Boot Washed Canvas - Limestone"]}]'
var kP = JSON.parse(k);

console.log(kP[0]['image:loc']);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64