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
  • 34,719
  • 24
  • 185
  • 251
Bob Jensen
  • 19
  • 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
  • 62,450
  • 9
  • 45
  • 52
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
  • 38,409
  • 6
  • 31
  • 57