Home > Net >  ASP classic and Json parse
ASP classic and Json parse

Time:04-20

I try to get value out from model_category and others like this in the JSON file.

I use aspJSON1.19.asp and my setup is:

For Each X In oJSON.data("result")
    Set this = oJSON.data("result").item(X)

    For Each item in this
        Response.Write item  & " : " & this.item(item) & "<br>"
    Next
Next

But I can't get values from items like model_category.

My JSON file contains:

{
    "result": [{
        "parent": "",
        "skip_sync": "false",
        "residual_date": "",
        "residual": "0",
        "sys_updated_on": "2022-04-08 07:41:11",
        "request_line": "",
        "sys_updated_by": "jan",
        "due_in": "",
        "model_category": {
            "link": "https://hest.service-now.com/api/now/table/cmdb_model_category/57d5bc14c3031000b959fd251eba8fd7",
            "value": "57d5bc14c3031000b959fd251eba8fd7"
        },
        "sys_created_on": "2021-09-28 13:31:33",
        "sys_domain": {
            "link": "https://hest.service-now.com/api/now/table/sys_user_group/global",
            "value": "global"
        },
        "u_owned_by_company": {
            "link": "https://hest.service-now.com/api/now/table/core_company/bff4eaf51b30201075fb7b75464bcb70",
            "value": "bff4eaf51b30201075fb7b75464bcb70"
        },
        "disposal_reason": "",
        "model": {
            "link": "https://hest.service-now.com/api/now/table/cmdb_model/6a6b3f731bb72050287b7d55464bcb99",
            "value": "6a6b3f731bb72050287b7d55464bcb99"
        },
        "u_correlation_id": "",
        "u_scanned_serial_number": "93619031",
        "install_date": "",
        "gl_account": "",
        "invoice_number": "",
        "sys_created_by": "manth",
        "warranty_expiration": "",
        "asset_tag": "",
        "depreciated_amount": "0",
        "substatus": "available",
        "pre_allocated": "false",
        "owned_by": "",
        "checked_out": "",
        "display_name": "Eizo - EV2456",
        "sys_domain_path": "/",
        "asset_function": "",
        "delivery_date": "4010-04-15 12:25:00",
        "retirement_date": "",
        "beneficiary": "",
        "install_status": "6",
        "cost_center": "",
        "supported_by": "",
        "assigned": "",
        "purchase_date": "",
        "work_notes": "",
        "managed_by": "",
        "sys_class_name": "alm_hardware",
        "sys_id": "01beedbe1bf6f410d37d8735464bcb50",
        "po_number": "6202",
        "stockroom": {
            "link": "https://hest.service-now.com/api/now/table/alm_stockroom/82fd1e941b8fec108d685532604bcba2",
            "value": "82fd1e941b8fec108d685532604bcba2"
        },
        "checked_in": "",
        "u_ritm_reference": {
            "link": "https://hest.service-now.com/api/now/table/sc_req_item/c7561ab41b72f41075fb7b75464bcb09",
            "value": "c7561ab41b72f41075fb7b75464bcb09"
        },
        "resale_price": "0",
        "vendor": "",
        "company": "",
        "retired": "",
        "justification": "",
        "department": "",
        "expenditure_type": "",
        "assigned_to": "",
        "depreciation_date": "",
        "old_status": "",
        "comments": "",
        "cost": "0",
        "quantity": "1",
        "acquisition_method": "",
        "sys_mod_count": "5",
        "old_substatus": "",
        "serial_number": "6677",
        "sys_tags": "",
        "u_warranty_start": "",
        "order_date": "",
        "support_group": "",
        "reserved_for": "",
        "due": "",
        "location": {
            "link": "https://hest.service-now.com/api/now/table/cmn_location/ef8b8b9b1b20b050287b7d55464bcbbf",
            "value": "ef8b8b9b1b20b050287b7d55464bcbbf"
        },
        "lease_id": "",
        "salvage_value": "0"
    }]
}

Any help?

CodePudding user response:

<!--#include virtual="/aspJSON1.19.asp" -->
<%
Sub PrintJson(item)
    Dim key
    
    response.write item
    If IsObject(item) Then
        If item Is Nothing Then Exit Sub
        If item.Count = 0 Then Exit Sub
        Response.Write "<ul jsonObject"">" & vbLf
        For Each key In item
            Response.Write "<li>" & vbLf
            Response.Write Server.HTMLEncode(key) & " : "
            PrintJson item(key)
            Response.Write "</li>"
        Next
        Response.Write "</ul>" & vbLf
    ElseIf IsArray(item) Then
        If UBound(item) < 0 Then Exit Sub
        Response.Write "<ul jsonArray"">" & vbLf
        For key = 0 To UBound(item)
            Response.Write "<li>" & vbLf
            Response.Write Server.HTMLEncode(key) & " : "
            PrintJson item(key)
            Response.Write "</li>" & vbLf
        Next
        Response.Write "</ul>" & vbLf
    ElseIf IsNull(item) Then
        Response.Write "<span jsonValue""><i>null</i></span>"
    Else
        Response.Write "<span jsonValue"">"
        Response.Write Server.HTMLEncode(item)
        Response.Write "</span>"
    End If
End Sub


  Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")

  oXMLHTTP.Open "GET", "https://....", False
  oXMLHTTP.Send

  
Set oJSON = New aspJSON
Set oJSON = aspJSON.loadJSON(oXMLHTTP.responseText)
PrintJson oJSON

But it gives a blank page

CodePudding user response:

That's because model_category is an object (a Scripting.Dictionary), and you can't Response.Write objects directly - you would have to write another For Each loop.

Use a recursive function instead:

Sub PrintJson(item)
    Dim key
    If IsObject(item) Then
        If item Is Nothing Then Exit Sub
        If item.Count = 0 Then Exit Sub
        Response.Write "<ul jsonObject"">" & vbLf
        For Each key In item
            Response.Write "<li>" & vbLf
            Response.Write Server.HTMLEncode(key) & " : "
            PrintJson item(key)
            Response.Write "</li>" & vbLf
        Next
        Response.Write "</ul>" & vbLf
    ElseIf IsArray(item) Then
        If UBound(item) < 0 Then Exit Sub
        Response.Write "<ul jsonArray"">" & vbLf
        For key = 0 To UBound(item)
            Response.Write "<li>" & vbLf
            Response.Write Server.HTMLEncode(key) & " : "
            PrintJson item(key)
            Response.Write "</li>" & vbLf
        Next
        Response.Write "</ul>" & vbLf
    ElseIf IsNull(item) Then
        Response.Write "<span jsonValue""><i>null</i></span>"
    Else
        Response.Write "<span jsonValue"">"
        Response.Write Server.HTMLEncode(item)
        Response.Write "</span>"
    End If
End Sub

Now you can do this:

Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "https://....", False
oXMLHTTP.Send

Set oJSON = New aspJSON
aspJSON.loadJSON(oXMLHTTP.responseText)

PrintJson oJSON.data

and it will print any nesting level. This is meant as a helper to visualize the content of e.g. a response from an API.


If you know the exact path to an item, and you know that this item exists (!), you can do access it directly

oJSON("result")(0)("model_category")("value")

But this will result in in an error if any of the keys do not exist on the object.

  • Related