1

I am trying to use aspJSON1.17.asp to get data from an JSON file that I am pulling back. ASPJson Site is located here now. http://web.archive.org/web/20160109181310/http://aspjson.com/

The structure of the file is as follows. NOTE: I have truncated here for readability

{
          "@odata.context": "http://trestle.corelogic.com/odata/$metadata#Property/CoreLogic.DataStandard.RESO.DD_1_5.Property",
          "value": [
            {
              "@odata.type": "#CoreLogic.DataStandard.RESO.DD_1_5.Property",
              "AboveGradeFinishedArea": null,
              "AboveGradeFinishedAreaSource": null,
              "AboveGradeFinishedAreaUnits": null,
              "AccessCode": null,
              "AccessibilityFeatures": null,
              "AdditionalParcelsDescription": null,
              "AdditionalParcelsYN": null,
              "AnchorsCoTenants": null,
              "Appliances": null,
              "ApprovalStatus": null,
              "ArchitecturalStyle": null,
              "AssociationAmenities": null,
              "AssociationFee": null,
              "AssociationFee2": null,
              "AssociationFee2Frequency": null,
              "AssociationFeeFrequency": null,
              "AssociationFeeIncludes": null,
              "AssociationName": null,
              "AssociationName2": null,
              "AssociationPhone": null,
              "AssociationPhone2": null,
              "AssociationYN": null,
              "AttachedGarageYN": null,
              "AvailabilityDate": null,
              "Basement": null,
              "BathroomsFull": 10
    }
  ]
}

I have tried the following where pagereturn is the JSON formatted string I am getting back from the API. I am trying to loop though this collection but I cannot get it to work. I keep getting an Object not a collection error.

Here is what I have tried

Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON(pageReturn)

'Loop through collection
For Each record In oJSON.data("Property")
    Set this = oJSON.data("Property").item(record)
    Response.Write _
    this.item("bathroomsFull") & "<br> "
Next

AND

'Loop through collection
For Each record In oJSON.data("bathroomsFull")
    Set this = oJSON.data("bathroomsFull").item(record)
    Response.Write _
    this.item("bathroomsFull") & "<br> "
Next

This did not give me an error but I got an empty sting

Response.Write "BathRooms"& oJSON.data("bathroomsFull") & "<br>"

Based on the above I would have thought that I would get "10"

Any help would be appreciated.

user692942
  • 16,398
  • 7
  • 76
  • 175
Rob
  • 333
  • 5
  • 25
  • You need to follow the structure of the json. In this case object -> property -> collection -> object. Had a similar question a while ago - https://stackoverflow.com/a/30574537/692942. – user692942 Jan 15 '18 at 22:20
  • @Lankymart thanks for the response. Based on your response I would have thought I would get something from this Response.Write "BathRooms"& oJSON.data("bathroomsFull") & "
    " but It does not return anything. Any suggestions
    – Rob Jan 15 '18 at 22:26
  • Why would that work? `oJSON.data()` is expecting the first level of the structure which is the anonymous object with a property called `value`, there is no `bathroomsFull` property at that level, does that make sence? – user692942 Jan 15 '18 at 22:35

1 Answers1

1

It’s just a case of understanding the structure of the json, then you can recreate the steps needed to pull the value in code.

The structure appears to be;

object
  property
    collection
      object
        property

So the code should be something like this;

Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON(pageReturn)

'Loop through collection
For Each record In oJSON.data("value")
  'Object Reference within the Array.
  Set this = oJSON.data("value").item(record)
  'Use the Object Reference to access it's properties.
  Response.Write this.item("BathroomsFull") & "<br> "
Next
user692942
  • 16,398
  • 7
  • 76
  • 175
  • @lankymark Thanks for the suggestion however, when I run that it still returns nothing. The actual output was



    which tells me that they are looping though the records but still not accessing the values. I see there are 4 objects returned when I use a JSON viewer.
    – Rob Jan 15 '18 at 22:37
  • @rob you will also need the correct case for the property, i’ve updated my example. Unfortunately i’m doung this from my mobile so not in a position to test it’s accuracy, but it should be close. If you are still struggling, try debugging at each level until you get what you are expecting. – user692942 Jan 15 '18 at 22:40
  • 1
    @Lankymark Thanks for the help. I was able to get the values back. I appreciate. it – Rob Jan 15 '18 at 22:46
  • @Rob glad to help. – user692942 Jan 15 '18 at 22:49