_VariantString cannot be cast to Dictionary

Hello all,

a problem that drive me crazy

It’s work :

var SagePrixSpec as RecordSet = SageSQLServer.SQLSelect("SELECT CT_Num as Client, AR_Ref as Ref, AC_PrixVen as PrixVente from F_ARTCLIENT WHERE AC_Categorie=0 AND AC_QteMont=0 ORDER BY CT_Num")

if SagePrixSpec <> NIL then
  
  AddMessageToListbox(DateTime.Now,"Extraction des Prix client")
  Var PrixSpecsJSON As New JSONItem
  
  while not SagePrixSpec.eof
    Var PrixSpecJSON As New JSONItem
    PrixSpecJSON.Value("client")= SagePrixSpec.Field("Client").StringValue.DefineEncoding(Encodings.UTF8)
    PrixSpecJSON.Value("ref")= SagePrixSpec.Field("Ref").StringValue.DefineEncoding(Encodings.UTF8)
    PrixSpecJSON.Value("prixvente") = SagePrixSpec.Field("PrixVente").StringValue.DefineEncoding(Encodings.UTF8)
    
    PrixSpecsJSON.Add(PrixSpecJSON)
    SagePrixSpec.moveNext
  wend
  SagePrixSpec.Close
  
  Var purifiedPrixSpecs as string = MakeValidUTF8(PrixSpecsJSON.ToString)
  Var PrixSpecs() as Variant = ParseJSON(purifiedPrixSpecs)
  
 
  For Each PrixSpec As Dictionary In PrixSpecs
    dbMysql.ExecuteSQL("INSERT INTO PrixSpec (Client,Ref,PrixVente) VALUES(?,?,?)",PrixSpec.Value("client"),PrixSpec.Value("ref"),PrixSpec.Value("prixvente"))
    
  Next
  
end if

Not working :

var SageContact as RecordSet = SageSQLServer.SQLSelect("SELECT "_
+"C.CT_Num AS Client,"_
+"ISNULL(C.CT_NOM, ' ') AS Nom,"_
+"ISNULL(C.CT_Prenom, ' ') AS Prenom,"_
+"ISNULL(C.CT_Telephone, ' ') AS Telephone,"_
+"ISNULL(C.CT_TelPortable, ' ') AS Portable,"_
+"ISNULL(C.CT_Email, ' ') AS Email,"_
+"ISNULL(C.CT_Fonction, ' ') AS Fonction "_
+"FROM F_CONTACTT C "_
+"JOIN F_COMPTET F ON C.CT_Num = F.CT_Num "_
+"WHERE F.CT_TYPE = 0 "_
+"ORDER BY C.CT_Num;")

if SageContact <> NIL then
  
  AddMessageToListbox(DateTime.Now,"Extraction des Contacts")
  Var ContactsClientJSON As New JSONItem
  
  while not SageContact.eof
    Var ContactJSON As New JSONItem
    ContactJSON.Value("client")= SageContact.Field("Client").StringValue.DefineEncoding(Encodings.UTF8)
    ContactJSON.Value("nom")= SageContact.Field("Nom").StringValue.DefineEncoding(Encodings.UTF8)
    ContactJSON.Value("prenom")= SageContact.Field("Prenom").StringValue.DefineEncoding(Encodings.UTF8)
    ContactJSON.Value("telephone")= SageContact.Field("Telephone").StringValue.DefineEncoding(Encodings.UTF8)
    ContactJSON.Value("portable")= SageContact.Field("Portable").StringValue.DefineEncoding(Encodings.UTF8)
    ContactJSON.Value("email")= SageContact.Field("Email").StringValue.DefineEncoding(Encodings.UTF8)
    ContactJSON.Value("fonction")= SageContact.Field("Fonction").StringValue.DefineEncoding(Encodings.UTF8)
    

    ContactsClientJSON.add(ContactJSON.ToString)
    
    SageContact.moveNext
  wend
  SageContact.Close
  
  Var purifiedContacts as string = MakeValidUTF8(ContactsClientJSON.ToString)
  Var ContactsClient() as Variant = ParseJSON(purifiedContacts)
  

  For Each Contact As Dictionary In ContactsClient
    dbMysql.ExecuteSQL("INSERT INTO Contact (Client,Nom,Prenom,Telephone,Portable,Email,Fonction) VALUES(?,?,?,?,?,?,?)",Contact.Value("client"),Contact.Value("nom"),Contact.Value("prenom"),Contact.Value("telephone"),Contact.Value("portable"),Contact.Value("email"),Contact.Value("fonction"))
    
  Next
  
  
end if

I use this code for other SQL request, and that’s work. But for the second code here, i got a IllegalCastException “_VariantString cannot be cast to Dictionary

If you have an idea :slight_smile:

What is the purpose of the above code?

MakeValidUTF8 (UTF-8 XML and weird characters - #3 by Kem_Tekinay)

To clean the json. Without this function : Invalid bytes in UTF8.

Is there a reason for using ToString in the second piece of code and not in the first?

The order doesn’t make sense. Clean the data first, make json next and don’t parse the json again.

OMFG :slight_smile:

I think i’m too tired… You are right… no reason

without .tostring, it’s work :slight_smile:

sometimes a second pair of eyes is useful :slight_smile:

Thank you

ps: like gremlins, no code after midnight lol