Parsing JSON Question

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Parsing JSON Question

Post by swhite »

Hi

I have the following JSON data. I use the LoadJSON to extract this JSON from a file but I am having some trouble determining which functions to use the process the data. I would like to grab the data from the first row and process it and then move to the next row etc.

[{"date":"20201103","exchtous":0.73456,"price":1.111,"pricecd":1.112,"pricefs":1.115,"product":"clrdsl","time":"00:00"},
{"date":"20201103","exchtous":0.73456,"price":2.111,"pricecd":2.112,"pricefs":2.115,"product":"reggas","time":"00:00"},
{"date":"20201103","exchtous":0.73456,"price":3.111,"pricecd":3.112,"pricefs":3.115,"product":"dyedsl","time":"00:00"}]
Simon White
dCipher Computing
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Parsing JSON Question

Post by cas »

Code: Select all

EnableExplicit

Structure json_data
  date.s
  exchtous.d
  price.d
  pricecd.d
  pricefs.d
  product.s
  time.s
EndStructure

Define raw_json.s = ~"[{\"date\":\"20201103\",\"exchtous\":0.73456,\"price\":1.111,\"pricecd\":1.112,\"pricefs\":1.115,\"product\":\"clrdsl\",\"time\":\"00:00\"}, {\"date\":\"20201103\",\"exchtous\":0.73456,\"price\":2.111,\"pricecd\":2.112,\"pricefs\":2.115,\"product\":\"reggas\",\"time\":\"00:00\"}, {\"date\":\"20201103\",\"exchtous\":0.73456,\"price\":3.111,\"pricecd\":3.112,\"pricefs\":3.115,\"product\":\"dyedsl\",\"time\":\"00:00\"}]"
Define json_obj=ParseJSON(#PB_Any,raw_json.s)

If json_obj
  NewList d.json_data()
  ExtractJSONList(JSONValue(json_obj), d())
  ResetList(d())
  ForEach d()
    Debug d()\product+" "+d()\price+" "+d()\date
  Next
EndIf
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Parsing JSON Question

Post by Paul »

swhite wrote:Hi

I have the following JSON data. I use the LoadJSON to extract this JSON from a file but I am having some trouble determining which functions to use the process the data. I would like to grab the data from the first row and process it and then move to the next row etc.

[{"date":"20201103","exchtous":0.73456,"price":1.111,"pricecd":1.112,"pricefs":1.115,"product":"clrdsl","time":"00:00"},
{"date":"20201103","exchtous":0.73456,"price":2.111,"pricecd":2.112,"pricefs":2.115,"product":"reggas","time":"00:00"},
{"date":"20201103","exchtous":0.73456,"price":3.111,"pricecd":3.112,"pricefs":3.115,"product":"dyedsl","time":"00:00"}]


Using the LoadJSON command...

Code: Select all

Structure jsondata
  date.s
  exchtous.d
  price.d
  pricecd.d
  pricefs.d
  product.s
  time.s
EndStructure
NewList dat.jsondata()

hJSON= LoadJSON(#PB_Any, "json.txt")
If hJSON
  ExtractJSONList(JSONValue(hJSON), dat() )            
  FreeJSON(hJSON)
EndIf


ForEach dat()
  Debug dat()\date
  Debug StrD(dat()\exchtous,5)
  Debug StrD(dat()\price,3)
  Debug StrD(dat()\pricecd,3)
  Debug StrD(dat()\pricefs,3)
  Debug dat()\product
  Debug dat()\time
  Debug "---"  
Next
Image Image
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Parsing JSON Question

Post by swhite »

Thank-you for these helpful responses.

Simon
Simon White
dCipher Computing
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Parsing JSON Question

Post by swhite »

Hi

I would like to be able to examine each JSON object in a JSON array but I do not see any function that allows me to do that as there is no GETJSONObject() or ExtractJSONObject(). I cannot use ExtractJSONList with a structure as this has to be a generic process where the structure is unknown. Is there a way to process each JSON object in the array. I have tried a number of things but each time the compiler says "A JSON value of type object is expected."

I have also tried the following where lcJSON is the data shown below but the array elements are empty

Code: Select all

Dim a.s(0)
loJSON = ParseJSON(#PB_Any,lcJSON)
ExtractJSONArray(JSONValue(loJSON),a())
Debug a(0)

Thanks,
Simon
[{"date":"20201103","exchtous":0.73456,"price":1.111,"pricecd":1.112,"pricefs":1.115,"product":"clrdsl","time":"00:00"},
{"date":"20201103","exchtous":0.73456,"price":2.111,"pricecd":2.112,"pricefs":2.115,"product":"reggas","time":"00:00"},
{"date":"20201103","exchtous":0.73456,"price":3.111,"pricecd":3.112,"pricefs":3.115,"product":"dyedsl","time":"00:00"}]
Simon White
dCipher Computing
Post Reply