简体   繁体   中英

undefined json field when making a get request on node.js

Im using an ocr api to scan the image. here's my get request

let request = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (data) {
      let JSONresponce = JSON.parse(data);
      console.log(JSONresponce.ParsedResults)
   });
});

request.on('error', function(e) {
    console.log('Cant scan the image');
});

request.end();

Here's the json structure i'm supposed to get

{
        "ParsedResults" : [
            {
                "TextOverlay" : {
                    "Lines" : [
                        {
                            "Words": [
                                {
                                "WordText": "Word 1",
                                "Left": 106,
                                "Top": 91,
                                "Height": 9,
                                "Width": 11
                                },
                                {
                                "WordText": "Word 2",
                                "Left": 121,
                                "Top": 90,
                                "Height": 13,
                                "Width": 51
                                }
                                .
                                .
                                .
                                More Words
                            ],
                            "MaxHeight": 13,
                            "MinTop": 90
                        },
                        .
                        .
                        .
                        .
                        More Lines
                    ],
                "HasOverlay" : true,
                "Message" : null
                },
                "FileParseExitCode" : "1",
                "ParsedText" : "This is a sample parsed result",
                                        
                "ErrorMessage" : null,
                "ErrorDetails" : null
            },
            {
                "TextOverlay" : null,
                "FileParseExitCode" : -10,
                "ParsedText" : null,
                                        
                "ErrorMessage" : "...error message (if any)",
                "ErrorDetails" : "...detailed error message (if any)"
            }
            .
            .
            .
            ],
        "OCRExitCode" : "2",
        "IsErroredOnProcessing" : false,
        "ErrorMessage" : null,
        "ErrorDetails" : null
        "SearchablePDFURL": "https://....." (if requested, otherwise null) 
        "ProcessingTimeInMilliseconds" : "3000"
    }

Since im logging only ParsedResults this is what im getting

[
  {
    TextOverlay: {
      Lines: [],
      HasOverlay: false,
      Message: 'Text overlay is not provided as it is not requested'
    },
    TextOrientation: '0',
    FileParseExitCode: 1,
    ParsedText: 'hello world',
    ErrorMessage: '',
    ErrorDetails: ''
  }
]

But the only thing i need is ParsedText field. And when i try to get it either via

JSONresponce.ParsedResults.ParsedText

or

JSONresponce.ParsedResults['ParsedText']

Im getting either just undefined response or an error msg with same meaning. Is there a way to get the ParsedText field ?

The JSON object is a array with one item. (Notice the [] characters) You need JSONresponce.ParsedResults[0]['ParsedText'] to get the first (and only) item from the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM