Advise on how to read the following json file

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Advise on how to read the following json file

Post by AndyGable »

Hi all

I have the following line coming in from my Server
{"messages":{"transactionId":["Invalid value: 5be3f476-f73e-4968-9ea5-721f622eaca2"]}}


but I can not seem to work out how I read in the information from the "transactionId"

i have this so far (and this works in other areas of my code

Private Sub GetStatusMessagesEOD(http As HttpClient)

    global.buffer = Null

    If http.Status < 0 Then 
        Global.addtoStatusList("Sorry a error was detected")
    Else
        ' Success - read data
        If Lof(http) Then Read #http, global.buffer, Lof(http)

        Dim vNew As Variant = JSON.Decode(global.buffer)
        
        global.AddToDebugList(global.buffer)
        Debug global.buffer
        

        
        If InStr(global.buffer, "error") Then
            Global.addtoStatusList("Terminal Offline or a Error has happened")
            MessageFunctions.SendToPoSterminal("Status|OFFLINE")
            'SendtoPoS Offline to PinPad (Show PoS would show the Chip&Pin Icon with the cross though it)
            
        Else
            Select Case LCase(vNew["messages"])
                Case "transactionid" 
                    Debug vNew["transactionId"][1]
            End Select   
        End If      
    End If


I need to send the "Invalid value: 5be3f476-f73e-4968-9ea5-721f622eaca2" back to my user as a message on screen but i can not for the love of me work this one out (sorry but my brain has given up today) maybe a fresh pair of eyes will help

Many thanks in advance

Andy
User avatar
BruceSteers
Posts: 1580
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Advise on how to read the following json file

Post by BruceSteers »

transactionid is an array of string[] and it is only one string , it is not a collection or an array so [1] does not exist

it is "Invalid value: 5be3f476-f73e-4968-9ea5-721f622eaca2"

EDIT: Also "messages" is a collection so you are inspecting it wrong.

You want something more like this...

For Each sKey In vNew["messages"].Keys  ' its a collection so read it as a collection
  Select LCase(sKey)
    Case "transactionid"
      sKey = vNew["messages"][sKey][0] ' get the data and find the name , data
      sName = Mid(sKey, 1, InStr(sKey, ":") - 1)
      sData = Mid(sKey, InStr(sKey, ":") + 2)
      Debug sName, sData
  End Select
Next
If at first you don't succeed , try doing something differently.
BruceS
Post Reply