4532066
4532066

Reputation: 2110

Object required: '[undefined]' error when looping through JSON data

I am working with the Google Translation API, which returns results in JSON format - e.g.

{
 "data": {
  "translations": [
   {
    "translatedText": "Hola mundo"
   },
   {
    "translatedText": "Te amo"
   },
   {
    "translatedText": "queso"
   }
  ]
 }
}

I am trying to parse the JSON data using Classic ASP.

I'm using ASPJSON (http://www.aspjson.com/) to parse the JSON data.

I can get so far with reading the data - e.g. (where "BackFromGoogle") is the objXML.responseText from a MSXML2.ServerXMLHTTP call.

Set oJSON = New aspJSON

oJSON.loadJSON(BackFromGoogle)

For Each translation In oJSON.data("data") 'iterate through data
    Set this = oJSON.data("data").item(translation)
Next    

If I then try:

For Each translation In oJSON.data("data") 'iterate through data
    Set this = oJSON.data("data").item(translation)
    Response.Write this.item("translations").item("translatedText")
Next    

Then I get this error:

Microsoft VBScript runtime error '800a01a8' Object required: '[undefined]'

For this line:

Response.Write this.item("translations").item("translatedText")

I am very stuck working out the syntax to allow me to access the individual values of the "translatedText" lines.

Is it possible to access them?

Upvotes: 0

Views: 1389

Answers (1)

4532066
4532066

Reputation: 2110

Got this working in the end.

Found the answer via the solution here: VbScript Deserialize JSON

This sorted it:

Set oJSON = New aspJSON

oJSON.loadJSON(BackFromGoogle)

For Each result In oJSON.data("data")("translations")
Set this = oJSON.data("data")("translations").item(result)
        response.Write this.item("translatedText") & "<br>"
Next

Upvotes: 2

Related Questions