Click here to Skip to main content
15,867,488 members
Articles / Web Development / ASP.NET / ASP.NETvNext
Tip/Trick

Easy JSON Recursion in VB.NET with Nested Levels

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
16 Mar 2017CPOL1 min read 24.2K   11   5
Easy JSON Recursion in VB.NET with nested levels

Introduction

This is an easy short JSON recursion solution in VB.NET that handles full multilevel json response files to pull values from the name fields...

Background

I was looking and couldn't find what I needed so I put some time to this. It may not be the best solution yet. But I can continue to work on it. But it was better than most I found though searching on the web. So this is a great little solution to find a value that may be below a bunch on nodes below the root of a json string.

Using the Code

Make sure you have the Newtonsoft.Json installed via NuGet.

Create a new Class in your project "JsonHelper":

VB.NET
//
// vb.net Class Code.
//
Imports System.IO
Imports Newtonsoft.Json.Linq

Public Class JsonHelper

    Private Shared output As String

    Public Shared Function GetJson_
        (res As String, txt As String, Optional Top As Integer = 0) As String

        Dim ser As JObject = JObject.Parse(res)

        Dim data() As JToken
        Dim jT() As JToken
        If Top = 0 Then
            data = ser.Children().ToArray()
            jT = data(0).First.ToArray()
        Else
            jT = ser.Children().ToArray()
        End If

        For Each itm As JProperty In jT

            If itm.Name = txt Then
                output = itm.Value.ToString
            End If

            If itm.Value.Type = JTokenType.Array Then
                Dim str As String = itm.First.First.ToString
                GetJson(str, txt, 1)
            End If
            If itm.Value.Type = JTokenType.Object Then
                Dim str As String = itm.First.ToString
                GetJson(str, txt, 1)
            End If

        Next

        Return output

    End Function

End Class

Given the following response returned from json:

JavaScript
    Transaction complete
  {
  "transaction": {
    "product": "CRUMP!",
    "id": "1asdsadasdsadasdsadsada",
    "location_id": "dfsdfsdfsdfsdfs",
    "created_at": "2017-03-10T13:43:47Z",
    "tenders": [
      {
        "type": "WOW",
        "id": "sdfsdfsdfsdfsdfsdfsd",
        "location_id": "fhghfghfghfghfghfghfgh",
        "transaction_id": "sfsdfsdfsdfsdfsdfsdfsdf",
        "created_at": "2017-03-10T13:43:47Z",
        "something": {
          "money": "CAD",
          "value": 234324234
        },
        "card_details": {
          "status": "DONE",
          "entry_method": "ONLINE",
          "DC": {
            "card_brand": "THINKIT",
            "last_4": "3232"
          }
        }
      }
    ]
  }
}

The Json is multilevel and getting to the child of the child of the child would be a pain and nobody wants to hardcode levels in. So given the JsonHelper...

All you have to do in your VB.NET application:

VB.NET
Dim trasid As String = JsonHelper.GetJson(response, "transaction_id")

Pass the Json to the helper in string format and request the feild name... and you will get the value returned no matter what level it's at.

History

  • V1.0 - rbettinelli - 03-15-2017 - New post - 1st version up

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Nottawasaga Valley Conservation Authority
Canada Canada
I have background in programming(many languages), web design, hardware and software. Currently I hold a lead database programming position for the Nottawasaga Valley Conservation Authority... Programming in vb.net and java comes 2nd nature to me and of course I do this for fun. I also used to design in cobol and pascal and run a BBS(anyone remember what this was?). Anyways, drop me a line maybe I can help you.

Comments and Discussions

 
QuestionGet ALL array results Pin
Ton Slijpen16-Mar-19 10:06
Ton Slijpen16-Mar-19 10:06 
QuestionQuestion Pin
Member 127954851-Oct-18 10:54
Member 127954851-Oct-18 10:54 
GeneralSlight change to allow for fields with same name but different child Pin
computer.us@aimint.org22-Jun-18 4:39
computer.us@aimint.org22-Jun-18 4:39 
QuestionToArray error with Sample Code Pin
Member 131760115-May-17 15:33
Member 131760115-May-17 15:33 
GeneralMy vote of 5 Pin
Rob Waibel Jr17-Mar-17 7:02
Rob Waibel Jr17-Mar-17 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.