Search code examples
arraysjsonvbscriptasp-classic

Get nested json value using ASP Classic and aspJSON


Good afternoon, I'm using aspJSON (https://github.com/rcdmk/aspJSON) to analyze a json that I have below:

{
   "pedido":1507WSC,
   "destino":"Brasil",
   "Passageiros":[
      {
         "bilhete":150WDE,
         "valor_seguro":0.0,
         "opcionais_bilhete":[
            {
               "tipo":"OET",
               "codigo":1502,
               "nome":"Esportiva",
               "valor":15.00
            }
         ],
         "codigo":528XCV,
      }
   ],
   "opcionais":null,
   "data_viagem":null
}

I am using the ASP code below to get some information.

Response.LCID = 1043

Dim objVoucher
Dim objJson : Set objJson = New JSONobject

Set objVoucher = objJson.parse(MyJasonTextHere)

Dim Pax

For Each Pax in objVoucher("Passageiros").items
        response.write (Pax.value("bilhete"))
Next

The result is 150WDE. It's correct so far.

However, now I need to get the information contained in the tipo parameter that is contained in the opcionais_bilhete node.

I've tried it in several ways but I always have an error. How do I get a value from a node that is inside the main json (nested json) in classic asp?

Thanks.


Solution

  • Expanding on my previous comment:

    Would it not be to use a For Each statement to loop through Pax("opcionais_bilhete").items as it's a JSONarray object?

    This works for me.

    <%
    Response.LCID = 1043
    
    Dim objVoucher
    Dim objJson : Set objJson = New JSONobject
    
    Set objVoucher = objJson.parse(json)
    
    Dim Pax, Op
    
    For Each Pax in objVoucher("Passageiros").items
      Call Response.Write(Pax.value("bilhete") & "<br />")
      'Loop through the "opcionais_bilhete" JSONarray
      For Each Op in Pax("opcionais_bilhete").items
        Call Response.Write(Op.value("tipo"))
      Next
    Next
    %>
    

    Output:

    150WDE
    OET