Android Question extract json fields that contain values

Makumbi

Well-Known Member
Licensed User
Please help how can i extract fields with values only example
HISEP1 :82
HISEP3 : 94
HISP1GRADE: 1
HISP1GRADE: 1
HISPOINTS: A

etc
How can i code this using b4a android
thanks in advance


B4X:
[{"ACCOUNT":"05-05828","Name":"ALIONZI MARTIN","HISEP1":"82","HISEP3":"94","HISEP6":null,"HISP1GRADE":"D1","HISP3GRADE":"D1","HISP6GRADE":"","GEOEP1":"51","GEOEP2":"73","GEOEP3":"88","GEOP1GRADE":"C6","GEOP2GRADE":"C3","GEOP3GRADE":"D1","ENGEP1":null,"ENGEP2":null,"ENGEP3":null,"ENGP1GRADE":"","ENGP2GRADE":"","ENGP3GRADE":"","ECONEP1":"73","ECONEP2":"88","ECONP1GRADE":"D2","ECONP2GRADE":"D1","DIVEP1":null,"DIVEP2":null,"DIVEP3":null,"DIVEP4":null,"DIVP1GRADE":"","DIVP2GRADE":"","DIVP3GRADE":"","DIVP4GRADE":"","LUGSWAEP1":null,"LUGSWAEP2":null,"LUGSWAEP3":null,"LUGSWAP1GRADE":"","LUGSWAP2GRADE":"","LUGSWAP3GRADE":"","FINEARTEP1":null,"FINEARTEP2":null,"FINEARTEP3":null,"FINEARTEP4":null,"FINEARTEP5":null,"FINEARTEP6":null,"FINEARTP1GRADE":"","FINEARTP2GRADE":"","FINEARTP3GRADE":"","FINEARTP4GRADE":"","FINEARTP5GRADE":"","FINEARTP6GRADE":"","ENTEP1":null,"ENTEP2":null,"ENTP1GRADE":"","ENTP2GRADE":"","COMPEP1":null,"COMPEP2":null,"COMP1GRADE":"","COMP2GRADE":"","SMATHEP1":"72","SMATHEP2":"84","SMATHP1GRADE":"C3","SMATHP2GRADE":"D1","GPEP1":"35","GPGRADE":"P8","PHYEP1":null,"PHYEP2":null,"PHYEP3":null,"PHYP1GRADE":"","PHYP2GRADE":"","PHYP3GRADE":"","CHEMEP1":null,"CHEMEP2":null,"CHEMEP3":null,"CHEMP1GRADE":"","CHEMP2GRADE":"","CHEMP3GRADE":"","BIOEP1":null,"BIOEP2":null,"BIOEP3":null,"BIOP1GRADE":"","BIOP2GRADE":"","BIOP3GRADE":"","MATHEP1":null,"MATHEP2":null,"MATHP1GRADE":"","MATHP2GRADE":"","AGRIEP1":null,"AGRIEP2":null,"AGRIEP3":null,"AGRIP1GRADE":"","AGRIP2GRADE":"","AGRIP3GRADE":"","HISPoints":"A","GPPoints":"F","PhyPoints":"","MathsPoints":null,"BioPoints":"","ChemPoints":"","GeogPoints":"D","EconPoints":"A","LitPoints":"","IREPoints":"","LugPoints":"","artpoints":"","FoodPoints":"","TDPoints":"","GermanPoints":"","WoodPoints":"","ArabPoints":"","EnterprPoints":null,"SubMathPoints":"O","CompPOINTS":"F","PrinciplePoints":15,"SubsidiaryPoints":1,"TotalPoints":16}]
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User


Try using this:
https://b4x.com:51041/json/index.html
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
how can i use this json code to extract out only those fields that contain values
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim BIOP1GRADE As String = colroot.Get("BIOP1GRADE")
 Dim GPGRADE As String = colroot.Get("GPGRADE")
 Dim COMPEP1 As String = colroot.Get("COMPEP1")
 Dim FINEARTP3GRADE As String = colroot.Get("FINEARTP3GRADE")
 Dim COMPEP2 As String = colroot.Get("COMPEP2")
 Dim ENTEP1 As String = colroot.Get("ENTEP1")
 Dim GEOEP1 As String = colroot.Get("GEOEP1")
 Dim ENTEP2 As String = colroot.Get("ENTEP2")
 Dim GEOEP3 As String = colroot.Get("GEOEP3")
 Dim TotalPoints As Int = colroot.Get("TotalPoints")
 Dim HISP1GRADE As String = colroot.Get("HISP1GRADE")
 Dim GEOEP2 As String = colroot.Get("GEOEP2")
 Dim WoodPoints As String = colroot.Get("WoodPoints")
 Dim ENGP3GRADE As String = colroot.Get("ENGP3GRADE")
 Dim GPPoints As String = colroot.Get("GPPoints")
 Dim FINEARTP4GRADE As String = colroot.Get("FINEARTP4GRADE")
 Dim HISPoints As String = colroot.Get("HISPoints")
 Dim FINEARTP2GRADE As String = colroot.Get("FINEARTP2GRADE")
 Dim FoodPoints As String = colroot.Get("FoodPoints")
 Dim PhyPoints As String = colroot.Get("PhyPoints")
 Dim FINEARTP5GRADE As String = colroot.Get("FINEARTP5GRADE")
 Dim FINEARTEP3 As String = colroot.Get("FINEARTEP3")
 Dim ECONEP1 As String = colroot.Get("ECONEP1")
 Dim FINEARTEP2 As String = colroot.Get("FINEARTEP2")
 Dim ECONEP2 As String = colroot.Get("ECONEP2")
 Dim FINEARTEP1 As String = colroot.Get("FINEARTEP1")
 Dim SubsidiaryPoints As Int = colroot.Get("SubsidiaryPoints")
 Dim HISEP6 As String = colroot.Get("HISEP6")
 Dim ENTP2GRADE As String = colroot.Get("ENTP2GRADE")
 Dim MATHEP1 As String = colroot.Get("MATHEP1")
 Dim MATHEP2 As String = colroot.Get("MATHEP2")
 Dim BIOP3GRADE As String = colroot.Get("BIOP3GRADE")
 Dim GEOP3GRADE As String = colroot.Get("GEOP3GRADE")
 Dim BIOEP2 As String = colroot.Get("BIOEP2")
 Dim HISP3GRADE As String = colroot.Get("HISP3GRADE")
 Dim BIOEP1 As String = colroot.Get("BIOEP1")
 Dim HISEP1 As String = colroot.Get("HISEP1")
 Dim HISEP3 As String = colroot.Get("HISEP3")
 Dim ENGEP3 As String = colroot.Get("ENGEP3")
 Dim ENGEP1 As String = colroot.Get("ENGEP1")
 Dim ENGEP2 As String = colroot.Get("ENGEP2")
 Dim PHYEP3 As String = colroot.Get("PHYEP3")
 Dim CHEMP1GRADE As String = colroot.Get("CHEMP1GRADE")
 Dim COMP1GRADE As String = colroot.Get("COMP1GRADE")
 Dim PHYEP2 As String = colroot.Get("PHYEP2")
 Dim PHYEP1 As String = colroot.Get("PHYEP1")
 Dim FINEARTP6GRADE As String = colroot.Get("FINEARTP6GRADE")
 Dim GermanPoints As String = colroot.Get("GermanPoints")
 Dim ENGP1GRADE As String = colroot.Get("ENGP1GRADE")
 Dim BioPoints As String = colroot.Get("BioPoints")
 Dim GPEP1 As String = colroot.Get("GPEP1")
 Dim SMATHP1GRADE As String = colroot.Get("SMATHP1GRADE")
 Dim MathsPoints As String = colroot.Get("MathsPoints")
 Dim PHYP2GRADE As String = colroot.Get("PHYP2GRADE")
 Dim GEOP2GRADE As String = colroot.Get("GEOP2GRADE")
 Dim ENTP1GRADE As String = colroot.Get("ENTP1GRADE")
 Dim BIOP2GRADE As String = colroot.Get("BIOP2GRADE")
 Dim DIVP2GRADE As String = colroot.Get("DIVP2GRADE")
 Dim AGRIP2GRADE As String = colroot.Get("AGRIP2GRADE")
 Dim LUGSWAP3GRADE As String = colroot.Get("LUGSWAP3GRADE")
 Dim MATHP2GRADE As String = colroot.Get("MATHP2GRADE")
 Dim ChemPoints As String = colroot.Get("ChemPoints")
 Dim HISP6GRADE As String = colroot.Get("HISP6GRADE")
 Dim Name As String = colroot.Get("Name")
 Dim DIVP3GRADE As String = colroot.Get("DIVP3GRADE")
 Dim ACCOUNT As String = colroot.Get("ACCOUNT")
 Dim ECONP2GRADE As String = colroot.Get("ECONP2GRADE")
 Dim LUGSWAP2GRADE As String = colroot.Get("LUGSWAP2GRADE")
 Dim AGRIEP2 As String = colroot.Get("AGRIEP2")
 Dim TDPoints As String = colroot.Get("TDPoints")
 Dim EnterprPoints As String = colroot.Get("EnterprPoints")
 Dim AGRIEP3 As String = colroot.Get("AGRIEP3")
 Dim DIVP4GRADE As String = colroot.Get("DIVP4GRADE")
 Dim CHEMP2GRADE As String = colroot.Get("CHEMP2GRADE")
 Dim GeogPoints As String = colroot.Get("GeogPoints")
 Dim AGRIEP1 As String = colroot.Get("AGRIEP1")
 Dim DIVP1GRADE As String = colroot.Get("DIVP1GRADE")
 Dim CompPOINTS As String = colroot.Get("CompPOINTS")
 Dim AGRIP3GRADE As String = colroot.Get("AGRIP3GRADE")
 Dim PHYP3GRADE As String = colroot.Get("PHYP3GRADE")
 Dim LUGSWAEP2 As String = colroot.Get("LUGSWAEP2")
 Dim LUGSWAEP3 As String = colroot.Get("LUGSWAEP3")
 Dim MATHP1GRADE As String = colroot.Get("MATHP1GRADE")
 Dim IREPoints As String = colroot.Get("IREPoints")
 Dim LUGSWAEP1 As String = colroot.Get("LUGSWAEP1")
 Dim SMATHEP2 As String = colroot.Get("SMATHEP2")
 Dim EconPoints As String = colroot.Get("EconPoints")
 Dim SMATHEP1 As String = colroot.Get("SMATHEP1")
 Dim BIOEP3 As String = colroot.Get("BIOEP3")
 Dim LugPoints As String = colroot.Get("LugPoints")
 Dim GEOP1GRADE As String = colroot.Get("GEOP1GRADE")
 Dim ArabPoints As String = colroot.Get("ArabPoints")
 Dim PrinciplePoints As Int = colroot.Get("PrinciplePoints")
 Dim FINEARTP1GRADE As String = colroot.Get("FINEARTP1GRADE")
 Dim FINEARTEP6 As String = colroot.Get("FINEARTEP6")
 Dim SubMathPoints As String = colroot.Get("SubMathPoints")
 Dim FINEARTEP5 As String = colroot.Get("FINEARTEP5")
 Dim AGRIP1GRADE As String = colroot.Get("AGRIP1GRADE")
 Dim FINEARTEP4 As String = colroot.Get("FINEARTEP4")
 Dim DIVEP2 As String = colroot.Get("DIVEP2")
 Dim DIVEP3 As String = colroot.Get("DIVEP3")
 Dim DIVEP1 As String = colroot.Get("DIVEP1")
 Dim LitPoints As String = colroot.Get("LitPoints")
 Dim ECONP1GRADE As String = colroot.Get("ECONP1GRADE")
 Dim PHYP1GRADE As String = colroot.Get("PHYP1GRADE")
 Dim DIVEP4 As String = colroot.Get("DIVEP4")
 Dim COMP2GRADE As String = colroot.Get("COMP2GRADE")
 Dim CHEMP3GRADE As String = colroot.Get("CHEMP3GRADE")
 Dim CHEMEP2 As String = colroot.Get("CHEMEP2")
 Dim CHEMEP3 As String = colroot.Get("CHEMEP3")
 Dim artpoints As String = colroot.Get("artpoints")
 Dim CHEMEP1 As String = colroot.Get("CHEMEP1")
 Dim ENGP2GRADE As String = colroot.Get("ENGP2GRADE")
 Dim LUGSWAP1GRADE As String = colroot.Get("LUGSWAP1GRADE")
 Dim SMATHP2GRADE As String = colroot.Get("SMATHP2GRADE")
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
using getvalueat and getkeyat should be avoided.

Another alternative:
B4X:
    Dim parser As JSONParser
    parser.Initialize(jsonString)
    Dim root As List = parser.NextArray
    For Each colroot As Map In root
        Dim dataMap As Map
        dataMap.Initialize
        For Each key As String In colroot.Keys
            If colroot.Get(key) <> Null And colroot.Get(key) <> "" Then
                Dim value As String = colroot.Get(key)
                dataMap.Put(key,value)
            End If
        Next
    Next
log(datamap)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since Makumbi is only interested in numeric values, shouldn't you add this:
B4X:
Dim value As String = colroot.Get(key)
  if isnumber(value) then
                dataMap.Put(key,value)
  end if
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
I attached a Zip file with the example, the result is this:


View attachment 80746
Thanks for the great effort mike and the entire B4a team it is bringing out what i wanted but my other problem is inserting the results into my table : How can i insert the results from the map into the table

My table Grades has fields like Account, Name,Subject,Score,Grades
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…