Jaya
Jaya

Reputation: 35

How to extract specific fields from json object and store it in a string in node.js

So I am working with Azure APIs, and receive a response in a JSON Object. I want to extract the "text" key and store the value in a string. For this example, the result should be, Hello World!

This is the output right now.

{
 "language": "en",
 "textAngle": 0.02617993877991553,
 "orientation": "Up",
 "regions": [
  {
   "boundingBox": "169,81,846,138",
   "lines": [
    {
     "boundingBox": "169,81,846,138",
     "words": [
      {
       "boundingBox": "169,81,348,126",
       "text": "Hello"
      },
      {
       "boundingBox": "570,92,445,127",
       "text": "World!"
      }
     ]
    }
   ]
  }
 ]
}

How do I extract only the texts in node js? Thankyou in advance

Upvotes: 0

Views: 722

Answers (1)

Jaya
Jaya

Reputation: 35

So i figured it out. Here is what i did. You gotta use nested loops to get it.

    for(let j=0; j<lines.length; j++){
        let words=data.regions[0].lines[j].words;
        for( let i=0; i<words.length; i++){
            final_string += words[i].text + " ";
        }
        line_string = final_string;
    }
console.log(line_string);

Upvotes: 1

Related Questions