I need to loop an javascript object array
This is my object response:
{
"kind": "calendar#events",
"etag": "\"p3288namrojte20g\"",
"summary": "pedicura",
"updated": "2019-05-01T14:25:51.642Z",
"timeZone": "America/Argentina/Cordoba",
"accessRole": "owner",
"defaultReminders": [
{
"method": "popup",
"minutes": 30
}
],
"nextSyncToken": "CJCLqtvE-uECEJCLqtvE-uECGAU=",
"items": [
{
"kind": "calendar#event",
"etag": "\"3113441344690000\"",
"id": "hb6me2h********1dun5rs10",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=aGI2bWUyaGFvaGtqbWI4bXYxZHVuNXJzMTAgZTYyMXJtMD********Zw",
"created": "2019-05-01T14:12:08.000Z",
"updated": "2019-05-01T14:24:32.345Z",
"summary": "Prueba de: Pedicura en sala 2-x",
"description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "pedicura",
"self": true
},
"start": {
"dateTime": "2019-05-08T21:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"end": {
"dateTime": "2019-05-08T23:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"iCalUID": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
},
{
"kind": "calendar#event",
"etag": "\"3113441378884000\"",
"id": "tv**0nbhkt**47la0k",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=dHZmOTQwbmJoa3Q3cnJvYjJsMW00N2xhMGsgZTYyMXJtM*****",
"created": "2019-05-01T14:18:53.000Z",
"updated": "2019-05-01T14:24:49.442Z",
"summary": "Prueba de: Pedicura en sala 2-x",
"description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "pedicura",
"self": true
},
"start": {
"dateTime": "2019-05-07T21:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"end": {
"dateTime": "2019-05-07T23:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"iCalUID": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
},
{
"kind": "calendar#event",
"etag": "\"3113441503284000\"",
"id": "tqhje1kd*****0",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=dHFoamUxa2Q0MG51M2Q3cGd2b2xlOGRqODAgZTYyMX*****",
"created": "2019-05-01T14:25:51.000Z",
"updated": "2019-05-01T14:25:51.642Z",
"summary": "Prueba de",
"description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "prueba",
"self": true
},
"start": {
"dateTime": "2019-04-26T21:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"end": {
"dateTime": "2019-04-26T23:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"iCalUID": "tqh*****[email protected]",
"sequence": 0,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
}
]
}
and i want to do something like this:
var dataStr = JSON.stringify(response);
var dataPrs = JSON.parse(dataStr);
var dataList = JSON.stringify(dataPrs.items);
dataStr = JSON.stringify(dataList);
dataPrs = JSON.parse(dataStr);
dataListPrs.forEach( console.log(dataPrs.htmlLink) );
1 Answer 1
You dont need to do JSON.stringify and JSON.parse everytime.
You need JSON.stringify only if you want to convert Javascript object into JSON string
and JSON.parse only if you want to convert JSON string to Javascript object and want to access its values with the key.
So, All you need is:
const data = {
"kind": "calendar#events",
"etag": "\"p3288namrojte20g\"",
"summary": "pedicura",
"updated": "2019-05-01T14:25:51.642Z",
"timeZone": "America/Argentina/Cordoba",
"accessRole": "owner",
"defaultReminders": [
{
"method": "popup",
"minutes": 30
}
],
"nextSyncToken": "CJCLqtvE-uECEJCLqtvE-uECGAU=",
"items": [
{
"kind": "calendar#event",
"etag": "\"3113441344690000\"",
"id": "hb6me2h********1dun5rs10",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=aGI2bWUyaGFvaGtqbWI4bXYxZHVuNXJzMTAgZTYyMXJtMD********Zw",
"created": "2019-05-01T14:12:08.000Z",
"updated": "2019-05-01T14:24:32.345Z",
"summary": "Prueba de: Pedicura en sala 2-x",
"description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "pedicura",
"self": true
},
"start": {
"dateTime": "2019-05-08T21:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"end": {
"dateTime": "2019-05-08T23:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"iCalUID": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
},
{
"kind": "calendar#event",
"etag": "\"3113441378884000\"",
"id": "tv**0nbhkt**47la0k",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=dHZmOTQwbmJoa3Q3cnJvYjJsMW00N2xhMGsgZTYyMXJtM*****",
"created": "2019-05-01T14:18:53.000Z",
"updated": "2019-05-01T14:24:49.442Z",
"summary": "Prueba de: Pedicura en sala 2-x",
"description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "pedicura",
"self": true
},
"start": {
"dateTime": "2019-05-07T21:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"end": {
"dateTime": "2019-05-07T23:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"iCalUID": "[email protected]",
"sequence": 2,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
},
{
"kind": "calendar#event",
"etag": "\"3113441503284000\"",
"id": "tqhje1kd*****0",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=dHFoamUxa2Q0MG51M2Q3cGd2b2xlOGRqODAgZTYyMX*****",
"created": "2019-05-01T14:25:51.000Z",
"updated": "2019-05-01T14:25:51.642Z",
"summary": "Prueba de",
"description": "x-prueba de añadir masajes en sala 2 y notificar al manicura y a otra persona, fernanda?",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "prueba",
"self": true
},
"start": {
"dateTime": "2019-04-26T21:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"end": {
"dateTime": "2019-04-26T23:00:00-03:00",
"timeZone": "America/Argentina/Buenos_Aires"
},
"iCalUID": "tqh*****[email protected]",
"sequence": 0,
"attendees": [
{
"email": "[email protected]",
"responseStatus": "needsAction"
},
{
"email": "[email protected]",
"responseStatus": "needsAction"
}
],
"reminders": {
"useDefault": true
}
}
]
}
const items = data.items
items.forEach(x => console.log(x.htmlLink))
answered May 1, 2019 at 15:18
Ashish
4,3801 gold badge20 silver badges46 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Alejandro Levit
i still can't, this is what i've tryed: ``` console.log("Response", response); var dataStr = JSON.stringify(response); var dataPrs = JSON.parse(dataStr); console.log("pse", dataPrs); var body = dataPrs.body; console.log("body", body); console.log("items", body.items); body.items.forEach(x => console.log(x.htmlLink)); ``` but body.items is undefined
Ashish
Why are you doing this:
var dataStr = JSON.stringify(response); var dataPrs = JSON.parse(dataStr); stringify and parse again and again.lang-js
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dataon this linedataPrs = JSON.parse(dataStr);