Below please find the JSON response. What I need is to get all "descriptions" returned in that JSON response object. To loop through and get the descriptions I need a way to get the index of, in my case, of the "textAnnotations".
In the following JSON 4 "textAnnotations" have been returned, How do I get this 4?
What I need is to get:
payload.responses[0].textAnnotations[0].description
payload.responses[0].textAnnotations[1].description
payload.responses[0].textAnnotations[2].description
payload.responses[0].textAnnotations[3].description
{
"responses": [
{
"textAnnotations": [
{
"locale": "en",
"description": "BR\nCOUNTRY\nERD6100",
"boundingPoly": {
"vertices": [
{
"x": 61,
"y": 247
},
{
"x": 461,
"y": 247
},
{
"x": 461,
"y": 359
},
{
"x": 61,
"y": 359
}
]
}
},
{
"description": "BR",
"boundingPoly": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
}
},
{
"description": "COUNTRY",
"boundingPoly": {
"vertices": [
{
"x": 253,
"y": 249
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 264
},
{
"x": 253,
"y": 263
}
]
}
},
{
"description": "ERD6100",
"boundingPoly": {
"vertices": [
{
"x": 95,
"y": 277
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
}
}
],
"fullTextAnnotation": {
"pages": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 0.4885855
},
{
"languageCode": "pt",
"confidence": 0.38148627
}
]
},
"width": 800,
"height": 600,
"blocks": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
},
"paragraphs": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
},
"words": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
},
"symbols": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 76,
"y": 339
},
{
"x": 75,
"y": 358
},
{
"x": 61,
"y": 358
}
]
},
"text": "B"
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 74,
"y": 339
},
{
"x": 86,
"y": 339
},
{
"x": 85,
"y": 358
},
{
"x": 73,
"y": 358
}
]
},
"text": "R"
}
]
}
]
}
],
"blockType": "TEXT"
},
{
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 247
},
{
"x": 461,
"y": 251
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
},
"paragraphs": [
{
"boundingBox": {
"vertices": [
{
"x": 253,
"y": 249
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 264
},
{
"x": 253,
"y": 263
}
]
},
"words": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "pt",
"confidence": 1
}
]
},
"boundingBox": {
"vertices": [
{
"x": 253,
"y": 249
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 264
},
{
"x": 253,
"y": 263
}
]
},
"symbols": [
{
"boundingBox": {
"vertices": [
{
"x": 253,
"y": 250
},
{
"x": 264,
"y": 250
},
{
"x": 264,
"y": 263
},
{
"x": 253,
"y": 263
}
]
},
"text": "B"
},
{
"boundingBox": {
"vertices": [
{
"x": 265,
"y": 250
},
{
"x": 277,
"y": 250
},
{
"x": 277,
"y": 263
},
{
"x": 265,
"y": 263
}
]
},
"text": "R"
},
{
"boundingBox": {
"vertices": [
{
"x": 279,
"y": 250
},
{
"x": 292,
"y": 250
},
{
"x": 292,
"y": 263
},
{
"x": 279,
"y": 263
}
]
},
"text": "A"
},
{
"boundingBox": {
"vertices": [
{
"x": 294,
"y": 250
},
{
"x": 303,
"y": 250
},
{
"x": 303,
"y": 263
},
{
"x": 294,
"y": 263
}
]
},
"text": "S"
},
{
"boundingBox": {
"vertices": [
{
"x": 306,
"y": 250
},
{
"x": 313,
"y": 250
},
{
"x": 313,
"y": 263
},
{
"x": 306,
"y": 263
}
]
},
"text": "I"
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 313,
"y": 250
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 263
},
{
"x": 313,
"y": 263
}
]
},
"text": "L"
}
]
}
]
},
{
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 277
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
},
"words": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 1
}
]
},
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 277
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
},
"symbols": [
{
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 278
},
{
"x": 142,
"y": 279
},
{
"x": 141,
"y": 349
},
{
"x": 94,
"y": 348
}
]
},
"text": "E"
},
{
"boundingBox": {
"vertices": [
{
"x": 149,
"y": 278
},
{
"x": 198,
"y": 279
},
{
"x": 197,
"y": 349
},
{
"x": 148,
"y": 348
}
]
},
"text": "R"
},
{
"boundingBox": {
"vertices": [
{
"x": 202,
"y": 279
},
{
"x": 248,
"y": 280
},
{
"x": 247,
"y": 350
},
{
"x": 201,
"y": 349
}
]
},
"text": "D"
},
{
"boundingBox": {
"vertices": [
{
"x": 260,
"y": 279
},
{
"x": 299,
"y": 279
},
{
"x": 298,
"y": 349
},
{
"x": 259,
"y": 349
}
]
},
"text": "6"
},
{
"boundingBox": {
"vertices": [
{
"x": 314,
"y": 280
},
{
"x": 352,
"y": 280
},
{
"x": 351,
"y": 350
},
{
"x": 313,
"y": 350
}
]
},
"text": "1"
},
{
"boundingBox": {
"vertices": [
{
"x": 366,
"y": 281
},
{
"x": 407,
"y": 281
},
{
"x": 406,
"y": 351
},
{
"x": 365,
"y": 351
}
]
},
"text": "0"
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 420,
"y": 281
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 351
},
{
"x": 419,
"y": 351
}
]
},
"text": "0"
}
]
}
]
}
],
"blockType": "TEXT"
}
]
}
],
"text": "BR\nCOUNTRY\nERD6100"
}
}
]
}
Assistance welcome. Paulo
CodePudding user response:
you can do something like this to extract the descriptions
const getDescriptions = data =>
data.flatMap(d => d.textAnnotations?
d.textAnnotations.map(t => t.description):
[]
)
const data = {
"responses": [
{
"textAnnotations": [
{
"locale": "en",
"description": "BR\nCOUNTRY\nERD6100",
"boundingPoly": {
"vertices": [
{
"x": 61,
"y": 247
},
{
"x": 461,
"y": 247
},
{
"x": 461,
"y": 359
},
{
"x": 61,
"y": 359
}
]
}
},
{
"description": "BR",
"boundingPoly": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
}
},
{
"description": "COUNTRY",
"boundingPoly": {
"vertices": [
{
"x": 253,
"y": 249
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 264
},
{
"x": 253,
"y": 263
}
]
}
},
{
"description": "ERD6100",
"boundingPoly": {
"vertices": [
{
"x": 95,
"y": 277
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
}
}
],
"fullTextAnnotation": {
"pages": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 0.4885855
},
{
"languageCode": "pt",
"confidence": 0.38148627
}
]
},
"width": 800,
"height": 600,
"blocks": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
},
"paragraphs": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
},
"words": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 86,
"y": 340
},
{
"x": 85,
"y": 359
},
{
"x": 61,
"y": 358
}
]
},
"symbols": [
{
"boundingBox": {
"vertices": [
{
"x": 62,
"y": 339
},
{
"x": 76,
"y": 339
},
{
"x": 75,
"y": 358
},
{
"x": 61,
"y": 358
}
]
},
"text": "B"
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 74,
"y": 339
},
{
"x": 86,
"y": 339
},
{
"x": 85,
"y": 358
},
{
"x": 73,
"y": 358
}
]
},
"text": "R"
}
]
}
]
}
],
"blockType": "TEXT"
},
{
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 247
},
{
"x": 461,
"y": 251
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
},
"paragraphs": [
{
"boundingBox": {
"vertices": [
{
"x": 253,
"y": 249
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 264
},
{
"x": 253,
"y": 263
}
]
},
"words": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "pt",
"confidence": 1
}
]
},
"boundingBox": {
"vertices": [
{
"x": 253,
"y": 249
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 264
},
{
"x": 253,
"y": 263
}
]
},
"symbols": [
{
"boundingBox": {
"vertices": [
{
"x": 253,
"y": 250
},
{
"x": 264,
"y": 250
},
{
"x": 264,
"y": 263
},
{
"x": 253,
"y": 263
}
]
},
"text": "B"
},
{
"boundingBox": {
"vertices": [
{
"x": 265,
"y": 250
},
{
"x": 277,
"y": 250
},
{
"x": 277,
"y": 263
},
{
"x": 265,
"y": 263
}
]
},
"text": "R"
},
{
"boundingBox": {
"vertices": [
{
"x": 279,
"y": 250
},
{
"x": 292,
"y": 250
},
{
"x": 292,
"y": 263
},
{
"x": 279,
"y": 263
}
]
},
"text": "A"
},
{
"boundingBox": {
"vertices": [
{
"x": 294,
"y": 250
},
{
"x": 303,
"y": 250
},
{
"x": 303,
"y": 263
},
{
"x": 294,
"y": 263
}
]
},
"text": "S"
},
{
"boundingBox": {
"vertices": [
{
"x": 306,
"y": 250
},
{
"x": 313,
"y": 250
},
{
"x": 313,
"y": 263
},
{
"x": 306,
"y": 263
}
]
},
"text": "I"
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 313,
"y": 250
},
{
"x": 323,
"y": 250
},
{
"x": 323,
"y": 263
},
{
"x": 313,
"y": 263
}
]
},
"text": "L"
}
]
}
]
},
{
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 277
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
},
"words": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 1
}
]
},
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 277
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 352
},
{
"x": 94,
"y": 348
}
]
},
"symbols": [
{
"boundingBox": {
"vertices": [
{
"x": 95,
"y": 278
},
{
"x": 142,
"y": 279
},
{
"x": 141,
"y": 349
},
{
"x": 94,
"y": 348
}
]
},
"text": "E"
},
{
"boundingBox": {
"vertices": [
{
"x": 149,
"y": 278
},
{
"x": 198,
"y": 279
},
{
"x": 197,
"y": 349
},
{
"x": 148,
"y": 348
}
]
},
"text": "R"
},
{
"boundingBox": {
"vertices": [
{
"x": 202,
"y": 279
},
{
"x": 248,
"y": 280
},
{
"x": 247,
"y": 350
},
{
"x": 201,
"y": 349
}
]
},
"text": "D"
},
{
"boundingBox": {
"vertices": [
{
"x": 260,
"y": 279
},
{
"x": 299,
"y": 279
},
{
"x": 298,
"y": 349
},
{
"x": 259,
"y": 349
}
]
},
"text": "6"
},
{
"boundingBox": {
"vertices": [
{
"x": 314,
"y": 280
},
{
"x": 352,
"y": 280
},
{
"x": 351,
"y": 350
},
{
"x": 313,
"y": 350
}
]
},
"text": "1"
},
{
"boundingBox": {
"vertices": [
{
"x": 366,
"y": 281
},
{
"x": 407,
"y": 281
},
{
"x": 406,
"y": 351
},
{
"x": 365,
"y": 351
}
]
},
"text": "0"
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"boundingBox": {
"vertices": [
{
"x": 420,
"y": 281
},
{
"x": 461,
"y": 281
},
{
"x": 460,
"y": 351
},
{
"x": 419,
"y": 351
}
]
},
"text": "0"
}
]
}
]
}
],
"blockType": "TEXT"
}
]
}
],
"text": "BR\nCOUNTRY\nERD6100"
}
}
]
}
console.log(getDescriptions(data.responses))
CodePudding user response:
You can use map to loop through the array as follows:
payload.responses[0].textAnnotations.map((textAnn, index) => {
return textAnn.description
})
You can either return or push desired value to array.