Parsing Nested Arrays using VBA and JSON -


i have json trying parse in vba. have parsed out array of "offers". inside array of "offers" array "prices""usd".

the problem not every "offers" object has "usd" array. trying create object can make table/sheet can't objects print in debug mode. works fails because not every dict offerdetails contains "usd" object.

what able print string , if "usd" object missing skip , print ones have "usd". have tried ismissing (in code) fails when hits missing "usd" object.

any idea how can string "usd" values? note "usd" array , contains several objects, don't know how address them either. ideally parse out "usd" same way did "offers". totally lost not in vba

this working script valid web json.

  sub getjsonep_lib_working()       'need jsonconverter found here https://github.com/vba-tools/vba-json       'need microsoft scripting runtime         dim parsed dictionary       dim item dictionary       dim offerdetails dictionary       dim price dictionary       dim usd dictionary                url = "http://wraymac.com/json/example1.json"               url2 = "[{" & """mpn""" & ":" & """41202""" & "}]"                set myrequest = createobject("winhttp.winhttprequest.5.1")               myrequest.open "get", url               myrequest.send                jsonstring = myrequest.responsetext                dim json object               set json = jsonconverter.parsejson(jsonstring)         set parsed = jsonconverter.parsejson(myrequest.responsetext)           each item in parsed("results")(1)("items")            each offerdetails in item("offers")         'i tried doesn't work, fails when finds non existent "usd"             if not ismissing(offerdetails("prices")("usd")(1)(1))             debug.print offerdetails("prices")("usd")(1)(1)             else             debug.print "missing"             end if         x = item("mpn") & "   " & "sku" & " - " & offerdetails("sku") & "," & "uid" & " - " & offerdetails("seller")("uid") & "   " & offerdetails("moq") & "packaging" & " = " & offerdetails("packaging") & "  " & offerdetails("seller")("name") & "  " & item("manufacturer")("name")       debug.print x        'this works fails because not every dict offerdetails contains "usd" object       'x = item("mpn") & "   " & "sku" & " - " & offerdetails("sku") & "," & "uid" & " - " & offerdetails("seller")("uid") & "   " & offerdetails("moq") & "packaging" & " = " & offerdetails("packaging") & "  " & offerdetails("seller")("name") & "  " & item("manufacturer")("name")& "  "&offerdetails("prices")("usd")(1)(1)         next offerdetails            next        end sub 

you want use dictionary's exists method:

set parsed = jsonconverter.parsejson(myrequest.responsetext)  each item in parsed("results")(1)("items")     each offerdetails in item("offers")              if offerdetails("prices").exists("usd")                 debug.print offerdetails("prices")("usd").count & " items:"                 debug.print "-----------------"                 each x in offerdetails("prices")("usd")                     debug.print x(1), x(2)                 next x                 debug.print "-----------------"             else                 debug.print "no usd"             end if       next offerdetails  next 

the converter parses objects ({}) dictionaries, , arrays ([]) collections, can use count determine number of items in each of types of object.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -