Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert JSON with key & value

From this JSON:
"{\"error\":false,\"parameters\":[{\"Name\":\"Vasanthakumar\"},{\"FirstName\":\"Vasanth\"},{\"LastName\":\"Kumar\"},{\"Phone\":\"1234567890\"}]}";


To this JSON
"{\"error\":false,\"parameters\":[{\"key\":\"Name\",\"value\":\"Vasanthakumar\"},{\"key\":\"FirstName\",\"value\":\"Vasanth\"},{\"key\":\"LastName\",\"value\":\"Kumar\"},{\"key\":\"Phone\",\"value\":\"1234567890\"}]}";


Conclusion:
Both Json I need to convert object like List of keyvalue pair <string, string="">
Ex: Key = Name
Value = Vasanthakumar

What I have tried:

I tried using Json converter but No luck to get success.
Posted
Updated 7-Aug-21 5:44am
v2
Comments
Richard MacCutchan 4-Aug-21 9:23am    
You first need to deserialize the source text, add the relevant fields to the classes and then serialize the result.
vasanthkumarmk 4-Aug-21 10:59am    
When I deseralize using List of keyvalue pair, the second json array looks correct order [0] => key = Name; Value=Vasanthakumar etc....
But the first json array looks [0] => [0][1] => key = key; Value = Name;
key = value; value = Vasanthakumar etc...
Richard MacCutchan 4-Aug-21 11:39am    
Sorry, we are not here to write people's code. MSDN contains plenty of code samples for serializing and deserializing JSON.

1 solution

Here is a sample program.

For clarity, I just manually created the source data with a class initialization.

The OP will mostly likely need to create the source data class instance with JsonConvert.DeserializeObject<sourceroot>(sourceJsonData) statement;

Here is a link to a tool that will create a C# class based on Json structures. It can be very helpful.

Code was built in Visual Studio 2019, and is a .Net 4.8 framework Console application.

C#
using Newtonsoft.Json;
using System.Collections.Generic;

namespace CodeProject_JSonConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            
            // This code block is to create sample data only. Use the JsonConvert.DeserializeObject<Root>(myJsonResponse) 
            // to initialize the sourceData class. 

            var sourceData = new SourceRoot
            {
                error = false,
                parameters = new List<SourceParameter>
                {
                    new SourceParameter {Name = "Vasanthakumar", FirstName = "Vasanth", LastName = "Kumar", Phone = "1234567890"}
                }
            };

            
            // This is the block of code that will convert the deserialized source class to the target class
            var targetData = new TargetRoot();
            targetData.parameters = new List<TargetParameter>();

            targetData.error = sourceData.error;
            foreach (var item in sourceData.parameters)
            {
                targetData.parameters.Add(new TargetParameter { key = nameof(SourceParameter.Name), value = item.Name });
                targetData.parameters.Add(new TargetParameter { key = nameof(SourceParameter.FirstName), value = item.FirstName });
                targetData.parameters.Add(new TargetParameter { key = nameof(SourceParameter.LastName), value = item.LastName });
                targetData.parameters.Add(new TargetParameter { key = nameof(SourceParameter.Phone), value = item.Phone });
            }

            // Serializes the target class into the new Json format
            var targetJson = JsonConvert.SerializeObject(targetData);
        }
    }

    public class SourceRoot
    {
        public bool error { get; set; }
        public List<SourceParameter> parameters { get; set; }
    }

    public class SourceParameter
    {
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
    }
    public class TargetRoot
    {
        public bool error { get; set; }
        public List<TargetParameter> parameters { get; set; }
    }

    public class TargetParameter
    {
        public string key { get; set; }
        public string value { get; set; }
    }
}


 
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