How to convert JSON to Dictionary

Hi Team,

How to convert JSON to Dictionary? Inside the JObject I can see multiple Object.

Sample JSON Object:
{
“Name”: “ABC”,
“Age”: 30,
“Location”: {
“City”: “Bangalore”,
“Country”: “India”
},
“Skills”: {
“Sport”: “Cricket”
“Technology”: “UiPath”
“Language”: “English”
}

I need to add all the Values to dictionary.
Name
Age
City
Country
Sport
Technology
Language

I need above value should add to Dictionary.

Hi!

Parse the string into a JObject
Then create a new dictionary and add the key-value pairs
Heres how you retrieve the values using the JObject:

dict("Name") = jsonObject("Name").ToString()
dict("Age") = jsonObject("Age").ToString()
dict("City") = jsonObject("Location")("City").ToString()
dict("Country") = jsonObject("Location")("Country").ToString()
dict("Sport") = jsonObject("Skills")("Sport").ToString()
dict("Technology") = jsonObject("Skills")("Technology").ToString()
dict("Language") = jsonObject("Skills")("Language").ToString()

I dont want to do any hardcode assign it should take automatically what ever the key have bot Take key and value from json

A generic parsing into a dictionary would work if that json was structured like this, ie just a list of key-value pairs with JsonConvert.DeserializeObject( of Dictionary(of String,Object)) ( yourJSONasAString)
{
“Name”: “ABC”,
“Age”: 30,
“City”: “Bangalore”,
“Country”: “India”,
“Sport”: “Cricket”,
“Technology”: “UiPath”,
“Language”: “English”
}
But since it’s nested you have to go the long way around

You could either do as I’ve mentioned or you have to use a coded workflow to define a new object and then use that object together with jsonconvert to parse your json to into that new object, and then retrieve the values from that new object.
How to deserialize JSON in C# - .NET | Microsoft Learn

@copy_writes,

Use this logic.

Variables:

Output:
The image displays the debugger output from a code execution environment, showing a dictionary with keys such as "Name," "Age," and "City," and their corresponding values. (Captioned by AI)

Code:
JSONDemo.xaml (12.6 KB)

Thanks,
Ashok :slight_smile:

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.