Click here to Skip to main content
15,916,318 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to convert Python into vb.net code? I am able to convert most of the code but I am not sure how to compare json value like 'title' or how to get accessURL as shown below in Python code


Phyton code

Python
url = "https://data.cms.gov/data.json"
title= "Specific Chronic Conditions"
time_period = "2017-01-01/2017-12-31"
response = requests.request("GET", url)

if response.ok:
 response = response.json()
 dataset = response['dataset']

 for set in dataset:
    if title == set['title']:
      for distro in set['distribution']:
        if 'format' in distro.keys():
          if distro['format'] == "API" and distro['temporal'] == time_period:
                 print(f"API link for {title} from {time_period} is
{distro['accessURL']}")


What I have tried:

I am able to get json string into string but I am not sure how to compare values just as "title" and get the accessurl based on title

VB Code

VB
Sub btnGetJson_Click(sender As Object, e As EventArgs)
  
      Dim URL1 = "https://data.cms.gov/data.json"
      Dim title = "Specific Chronic Conditions"
      Dim time_period = "2017-01-01/2017-12-31"

        Dim url As String = URL1
        Dim webRequest As WebRequest = webRequest.Create(url)
        Dim request As HttpWebRequest = CType(webRequest, HttpWebRequest)
        request.Method = "GET"
        request.ContentType = "application/json"

        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim jsonString As String = ""

        Using reader As StreamReader = New StreamReader(response.GetResponseStream())
            jsonString = reader.ReadToEnd()
        End Using

    End Sub
Posted
Updated 7-Dec-22 10:06am

1 solution

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

In this case, I'd strongly suggest that you look at Newtonsoft.Json (you can install it to your project via NuGet) and use that to convert the JSON data to classes you VB code can use directly.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900