I have following Json file which i want to convert to a different format. New format shown below. Can we use Json Parsing for this? or can we use Regular expression to change the modify the json string.
We can also use custom json deserializer i think. Can someone help me with some easy way to do this?
Current format
{
"PRETEST":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"2XX"},
"c":{"source":"VeD","name":"c","value":"4933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"EHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"HU55"},
"g":{"source":"VeD","name":"g","value":"453"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200},
"TEST":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"3XX"},
"c":{"source":"VeD","name":"c","value":"5933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"FHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"HU55"},
"g":{"source":"VeD","name":"g","value":"433"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200},
"INT":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"4XX"},
"c":{"source":"VeD","name":"c","value":"1933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"KHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"KU55"},
"g":{"source":"VeD","name":"g","value":"253"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200}}
New Format
{
"PRETEST":
{"response":
{"a"null,
"b":"2XX",
"c":"4933011630372431565180",
"d"::null,
"e":"EHD453REN00000004",
"f":"HU55",
"g":"453",
"h"::null,
"i"::null},
"httpcode":200},
"TEST":
{"response":
{"a":null,
"b""3XX"
"c":"5933011630372431565180",
"d":null,
"e""FHD453REN00000004",
"f""HU55",
"g""433",
"h":null,
"i":null},
"httpcode":200},
"INT":
{"response":
{"a":null,
"b""4XX",
"c":"1933011630372431565180",
"d":null,
"e":"KHD453REN00000004",
"f":"KU55",
"g":"253",
"h":null,
"i":null},
"httpcode":200},
"PREPROD":
{"response":
{"a":null,
"b":"5XX",
"c":"8933011630372431565180",
"d":null,
"e":"EHD453REN00000004",
"f":"HU55",
"g":"453",
"h":null,
"i":null},
"httpcode":200}}
Here is a regular expression solution.
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\{[^{}]+value\":([^{}]+)\}";
string replacement = "$1";
Regex rgx = new Regex(pattern);
string input = YOUR_JSON_STRING;
string result = rgx.Replace(input, replacement);
Console.WriteLine(result);
}
}