1

I need to parse this type of JSON string and place it database tables eventually. I was thinking :1. parse it into vb .net classes (object) than store it in the tables. I have Newtown imported into my .net but not very familiar with it to be able to break the json to objects.

{
    "phenotype_results": [
        {
            "gName": "CYP2",
            "gtype": "*11",
            "otype": "normal",
            "adjustedPhe": "poor",
            "note": "string"
        },
        {
            "gName": "CYP2",
            "gtype": "*113",
            "otype": "normal",
            "adjustedPhe": "poor",
            "note": "string"
        },
        {
            "gName": "CYP2",
            "gtype": "**1",
            "otype": "normal",
            "adjustedPhe": "poor",
            "note": "string"
        },
        
    ],
    "medics": [
        {
            "mName": "flax",
            "Adj": [
                {
                    "eName": "CYP",
                    "eType": null,
                    "substrate": true
                },
                {
                    "eName": "CYP3",
                    "eType": null,
                    "substrate": true
                },
                {
                    "eName": "CYP4",
                    "eType": null,
                    "substrate": true
                },
            ],
            "associations": [
                "CYP20"
            ],
            "recommendation": "No action required. .",
            "strength": "",
            "source": "DPWG",
            "source_url": "https://www.pha.xom",
            "pathway_url": "https://www.xxx.yyy"
        },
        {
            "newName": "carba",
            "geneAdjustments": [
                {
                    "geneName": "CYP2B6",
                    "adjType": "Inducer",
                    "substrate": false
                },
                {
                    "geneName": "CYP3A5",
                    "adjType": "Inducer",
                    "substrate": true
                },
                {
                    "eName": "CYP4",
                    "eType": null,
                    "substrate": true
                }                
            ],
            "assoc": [
                "HLA-A",
                "HLA-B"
            ],
            "rec": "If cccc do yyy .",
            "strength": "STRONG",
            "source": "CPIC",
            "source_url": "https://www.p",
            "pathway_url": "https://www"
        }
    ],
    "recommendations": [
        {
            "associ": [
                "HLA1"
            ],
            "recommendation": "Use per stand .",
            "strength": "STRONG",
            "medName": "vir",
            "source": "CPIC",
            "actionable": false,
            "medType": "Anti-Infective"
         
        },
         {
            "associ": [
                "HLA1"
            ],
            "recommendation": "Use per stand .",
            "strength": "STRONG",
            "medName": "vir",
            "source": "CPIC",
            "actionable": false,
            "medType": "Anti-Infective"
         
        }
        
    ]
}

See json file above and let me know if you have any questions.

Thanks

Charlieface
  • 52,284
  • 6
  • 19
  • 43
AviZiyah
  • 11
  • 1

2 Answers2

1

So first you need to define you objects. If you don't already have the classes defined, I would cheat and use this tool. I am lazy like that.

https://json2csharp.com/

Once you have your object graph, then you can use Entity Framework to persist them into your database. I am not very good with that, personally, so I will defer to others here to help out with that step.

Peter Lange
  • 2,786
  • 2
  • 26
  • 40
0

Create a Json object as below and extract the desired data.

You are free to use any tools.

 Try
         'your json
         Dim jsonString = System.IO.File.ReadAllText("D:\Json.txt")
         Dim dto As TestDTO

         dto = JsonConvert.DeserializeObject(Of TestDTO)(jsonString)

        'Deserialize DataSet
        'Dim dto As DataSet
        'dto = JsonConvert.DeserializeObject(Of DataSet)(jsonString)

         Dim phenotypeList As List(Of phenotype_resultsDTO) = dto.phenotype_results

         For i = 0 To phenotypeList.Count - 1

            Debug.WriteLine(phenotypeList(i).gName)
            Debug.WriteLine(phenotypeList(i).gtype)

         Next
      Catch ex As Exception
         MsgBox(ex.ToString)
      End Try

'JSON Sample

Public Class TestDTO

   Public Property phenotype_results As IList(Of phenotype_resultsDTO)
   Public Property medics As IList(Of medicslDTO)

End Class

Public Class phenotype_resultsDTO

   Public Property gName As String
   Public Property gtype As String

End Class

Public Class medicslDTO

   Public Property mName As String
   Public Property Adj As IList(Of AdjDTO)

End Class

Public Class AdjDTO

End Class

   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


      Try

         'your json
         '    Dim jsonString = System.IO.File.ReadAllText("D:\Json.txt")

         Dim jsonString As String = " {'actions' : { 'VRA-B*58:00': [ 'allopu' ], 'VKO1': [ 'film1' ], 'CYP2': [ 'song2' ], 'HLA-3': [ 'dance3', 'dance4' ], 'ETC': [ 'tac' ] }} "


         Dim myDeserializedClass As Roo = JsonConvert.DeserializeObject(Of Roo)(jsonString)


         Dim action As Actions = myDeserializedClass.actions


         For Each item As String In action.VRAB5800
            Console.WriteLine(item)
         Next

         For Each item As String In action.VKO1
            Console.WriteLine(item)
         Next

         For Each item As String In action.CYP2
            Console.WriteLine(item)
         Next

         For Each item As String In action.HLA3
            Console.WriteLine(item)
         Next

         For Each item As String In action.ETC
            Console.WriteLine(item)
         Next

      Catch ex As Exception
         MsgBox(ex.ToString)
      End Try


   End Sub


End Class


Public Class Actions
   <JsonProperty("VRA-B*58:00")>
   Public Property VRAB5800 As List(Of String)
   Public Property VKO1 As List(Of String)
   Public Property CYP2 As List(Of String)
   <JsonProperty("HLA-3")>
   Public Property HLA3 As List(Of String)
   Public Property ETC As List(Of String)
End Class

Public Class Roo
   Public Property actions As Actions
End Class

Think2826
  • 201
  • 1
  • 7
  • the part that I'm not clear is, since my jSon string is long and complex with nested objects that sore are arrays and some other objects. Should I try to break it down to more smaller items. How do i make sure that it will deserialize correctly? – AviZiyah Jul 04 '21 at 23:03
  • Sorry I don't know your question. When I go back to the beginning and see your question, you can easily convert your Json to a Dataset. I edited the answer. – Think2826 Jul 05 '21 at 01:59
  • Yes, i was able to do so fine. ty. However i have this :"actions": { "VRA-B*58:00": [ "allopu" ], "VKO1": [ "film1" ], "CYP2": [ "song2" ], "HLA-3": [ "dance3", "dance4" ], "ETC": [ "tac" ] } Do you know if i can deserialize it to an array of strings and how? – AviZiyah Jul 06 '21 at 18:07
  • I tried parsing the json you requested using an automatic tool. check my answer – Think2826 Jul 07 '21 at 00:52
  • It's all good. ty – AviZiyah Jul 14 '21 at 15:09