Convert nested JSON to plain JSON

I am trying to convert nested json to plain json by recursively traversing. (Json input structure is unknown)

for example i want json to be like this

{
    "FirstName": "Rahul",
    "LastName": "B",
    "EmpType": {
        "RID": 2,
        "Title": "Full Time"
    },
    "CTC": "3.5",
    "Exp": "1",
    "ComplexObj": {
        "RID": 3,
        "Title": {
            "Test": "RID",
            "TWO": {
                "Test": 12
            }
        }
    }
}

to convert something like this

{
    "FirstName": "Rahul",
    "LastName": "B",
    "EmpType__RID": 2,
    "EmpType__Title": "Full Time",
    "CTC": "3.5",
    "Exp": "1",
    "ComplexObj__RID": 3,
    "ComplexObj__Title__Test": "RID",
    "ComplexObj__Title__TWO__Test": 12
}

each field of the embedded object will be changed to a key that represents its actual path.

this is what i have done so far.

    public static void ConvertNestedJsonToSimpleJson(JObject jobject, ref JObject jobjectRef, string currentNodeName = "", string rootPath = "")
    {
        string propName = "";
        if (currentNodeName.Equals(rootPath))
        {
            propName = currentNodeName;
        }
        else
        {
            propName = (rootPath == "" && currentNodeName == "") ? rootPath + "" + currentNodeName : rootPath + "__" + currentNodeName;
        }

        foreach (JProperty jprop in jobject.Properties())
        {
            if (jprop.Children<JObject>().Count() == 0)
            {
                jobjectRef.Add(propName == "" ? jprop.Name : propName + "__" + jprop.Name, jprop.Value);
            }
            else
            {
                currentNodeName = jprop.Name;
                rootPath = rootPath == "" ? jprop.Name : rootPath;
                ConvertNestedJsonToSimpleJson(JObject.Parse(jprop.Value.ToString()), ref jobjectRef, currentNodeName, rootPath);
            }
        }
    }

and get the wrong result

{
    "FirstName": "Rahul",
    "LastName": "B",
    "EmpType__RID": 2,
    "EmpType__Title": "Full Time",
    "CTC": "3.5",
    "Exp": "1",
    "EmpType__ComplexObj__RID": 3,
    "EmpType__Title__Test": "RID",
    "EmpType__two__Test": 12
}

would appreciate any help on fixing my code or any other approach to archiving this.

+4
source share
3 answers
  • You do not need to convert the property value to a string and then parse it again each time - just enter it in JObject
  • - : prefix + jprop.Name + "__"

:

public static void FlattenJson(JObject node, JObject result, string prefix = "")
{
    foreach (var jprop in node.Properties())
    {
        if (jprop.Children<JObject>().Count() == 0)
        {
            result.Add(prefix + jprop.Name, jprop.Value);
        }
        else
        {
            FlattenJson((JObject)jprop.Value, $"{prefix}{jprop.Name}__", result);
        }
    }
}

:

var node = JObject.Parse(/* the input string */);
var result = new JObject();
FlattenJson(node, result);
+4

rootPath = rootPath == "" ? jprop.Name : rootPath;. rootPath, EmpType, , ComplexObj rootPath . , , .

, root currentnode . node , :

public static void ConvertNestedJsonToSimpleJson(JObject input, JObject output, string prefix = "")
{
    foreach (JProperty jprop in input.Properties())
    {
        var name = prefix==""?jprop.Name:String.Format("{0}__{1}", prefix,jprop.Name);
        if (jprop.Children<JObject>().Count() == 0)
        {
            output.Add(name, jprop.Value);
        }
        else
        {
            ConvertNestedJsonToSimpleJson((JObject)jprop.Value, output, name);
        }
    }
}

:

{
  "FirstName": "Rahul",
  "LastName": "B",
  "EmpType__RID": 2,
  "EmpType__Title": "Full Time",
  "CTC": "3.5",
  "Exp": "1",
  "ComplexObj__RID": 3,
  "ComplexObj__Title__Test": "RID",
  "ComplexObj__Title__TWO__Test": 12
}

.

+4

- linq

var jsonObj = jobject.select(x => new CustomJson {
   FirstName = x.FirstName,
   LastName = x.LastName,
   EmpTypeId = x.EmpType.Id,
   Title = x.EmpType.Title
   etc etc
});
+1

Source: https://habr.com/ru/post/1666987/


All Articles