0

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?

phuzi
  • 12,078
  • 3
  • 26
  • 50
jama
  • 1

1 Answers1

2

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 class=""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 class=""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 class=""jsonValue""><i>null</i></span>"
    Else
        Response.Write "<span class=""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
oJSON.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.

user692942
  • 16,398
  • 7
  • 76
  • 175
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • It doesn't need a loop if the value is an object, just call into the object reference using `.Item()`. Nice function though. – user692942 Apr 19 '22 at 11:32
  • @user692942 It needs a loop if it is an object. Every object member could be another object or array, after all. That's what `PrintJson item(key)` does. – Tomalak Apr 19 '22 at 11:51
  • If you are parsing a structure you know, you don't need a loop necessarily. You need a loop if the object property is an Array, otherwise `this.item().item("model_category").item("value")` (`` refers to the instance of the object inside the `For Each` statement) will return the objects `value` property for example. In the OPs JSON structure, only the `results` property is an Array. – user692942 Apr 19 '22 at 11:55
  • @user692942 ...but I'm not trying to get to a certain leaf value with this function. This function is meant to print the entire object graph, no matter what it looks like, as a way of visualizing it. *(For the OP: Yes, if you know the exact path to an item, and you know that this item exists, you can do `oJSON("result")(0)("model_category")("value")`.)* – Tomalak Apr 19 '22 at 12:05
  • I know you're not (why I said "Nice function though"). Just think "you would have to write another For Each loop" is a bit misleading hence [my original comment](https://stackoverflow.com/questions/71923814/asp-classic-and-json-parse#comment127093352_71924148). – user692942 Apr 19 '22 at 12:07
  • This is my page.asp <% Sub PrintJson(item) ........ 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 me a blank page – jama Apr 19 '22 at 12:11
  • 1
    @jama The function above is tested and works. If it prints nothing for you, it's because because your `oJSON` is probably empty. – Tomalak Apr 19 '22 at 12:47
  • 1
    @Tomalak in the version of the library linked by th OP, `loadJson` is a sub, so it won't return anything. I think the code should be : `aspJSON.loadJSON("...") : set oJson = aspJSON.data` – Flakes Apr 19 '22 at 16:23
  • 1
    @Flakes Good catch, I haven't checked that. But that explains it. – Tomalak Apr 19 '22 at 16:27
  • 2
    @jama See the comment from Flakes and the updated answer. You need to do `PrintJson oJSON.data`. – Tomalak Apr 19 '22 at 16:28
  • Microsoft VBScript-runl error '800a01f4' Variablen is not defineret: 'aspJSON' in this line : aspJSON.loadJSON(oXMLHTTP.responseText) (In danish ;) ) If i do response.write oXMLHTTP.responseText it print resultat of json file – jama Apr 19 '22 at 22:14
  • 1
    find the error :) oJSON not aspJSON in this line aspJSON.loadJSON(oXMLHTTP.responseText) – jama Apr 19 '22 at 22:39
  • The script work fine, but if i will put it in a table like or how do it with PrintJson and where to insert (next post) if i have more then 1 post – jama Apr 20 '22 at 08:22
  • @jama You'll figure out how to structure your HTML, I'm sure. – Tomalak Apr 20 '22 at 08:26
  • But where in the script does it go to next post, or can i check when counter change ? – jama Apr 20 '22 at 08:32
  • @jama It prints the entire JSON. It's meant to be a debugging help, not a way to write individual items into a table. You would still have to write the loops that do that yourself. But everything you need for that is in this function, take it apart and re-write it. – Tomalak Apr 20 '22 at 08:41