Input json is (The json is little part of real data, real json is very long and more hierarchy. json line more than 30k)
{
"data": {
"getUsers": [
{
"userProfileDetail": {
"userStatus": {
"name": "Expired"
},
"userStatusDate": "2017-04-04T07:48:25+00:00",
"lastAttestationDate": "2019-02-01T03:50:42.6049634-05:00"
},
"userInformation": {
"Id": 13610875,
"lastName": "************",
"suffix": null,
"gender": "FEMALE",
"birthDate": "1970-01-01T00:01:00+00:00",
"ssn": "000000000",
"ethnicity": "INVALID_REFERENCE_VALUE",
"languagesSpoken": null,
"personalEmail": null,
"otherNames": null,
"userType": {
"name": "APN"
},
"primaryuserState": "CO",
"otheruserState": [
"CO"
],
"practiceSetting": "INPATIENT_ONLY",
"primaryEmail": "*****@*****.com"
}
},
{
"userProfileDetail": {
"userStatus": {
"name": "Expired newwwwwwwwwwww"
},
"userStatusDate": "2017-04-04T07:48:25+00:00",
"lastAttestationDate": "2019-02-01T03:50:42.6049634-05:00"
},
"userInformation": {
"Id": 13610875,
"lastName": "************",
"suffix": null,
"gender": "FEMALE",
"birthDate": "1970-01-01T00:01:00+00:00",
"ssn": "000000000",
"ethnicity": "INVALID_REFERENCE_VALUE",
"languagesSpoken": null,
"personalEmail": null,
"otherNames": null,
"userType": {
"name": "APN"
},
"primaryuserState": "CO",
"otheruserState": [
"CO"
],
"practiceSetting": "INPATIENT_ONLY",
"primaryEmail": "*****@*****.com"
}
}
]
}
}
the code is
var obj = JObject.Parse(json);
// Collect column titles: all property names whose values are of type JValue, distinct, in order of encountering them.
var jsonValues = obj.DescendantsAndSelf().OfType<JProperty>().Where(p => p.Value is JValue).GroupBy(p => p.Name).ToList();
var jsonKey = jsonValues.Select(g => g.Key).ToArray();
// Filter JObjects that have child objects that have values.
var parentsWithChildren = jsonValues.SelectMany(g => g).SelectMany(v => v.AncestorsAndSelf().OfType<JObject>().Skip(1)).ToHashSet();
// Collect all data rows: for every object, go through the column titles and get the value of that property in the closest ancestor or self that has a value of that name.
var rows = obj
.DescendantsAndSelf()
.OfType<JObject>()
.Where(o => o.PropertyValues().OfType<JValue>().Any() && (o == obj || !parentsWithChildren.Contains(o))) // Show a row for the root object + objects that have no children.
.Select(o => jsonKey.Select(c => o.AncestorsAndSelf().OfType<JObject>().Select(parent => parent[c])
.Where(v => v is JValue).Select(v => (string)v).FirstOrDefault()).Reverse() // Trim trailing nulls
.SkipWhile(s => s == null).Reverse());
// Convert to CSV
var csvRows = new[] { jsonKey }.Concat(rows).Select(r => string.Join(",", r));
var csv = string.Join("\n", csvRows);
Console.WriteLine(csv);
Here is the output i get:
getUsers_userProfileDetail_userStatus_name,getUsers_userProfileDetail_userStatusDate,getUsers_userProfileDetail_lastAttestationDate,getUsers_userInformation_Id,getUsers_userInformation_lastName,getUsers_userInformation_suffix,getUsers_userInformation_gender,getUsers_userInformation_birthDate,getUsers_userInformation_ssn,getUsers_userInformation_ethnicity,getUsers_userInformation_languagesSpoken,getUsers_userInformation_personalEmail,getUsers_userInformation_otherNames,getUsers_userInformation_userType_name,getUsers_userInformation_primaryuserState,getUsers_userInformation_otheruserState,getUsers_userInformation_practiceSetting,getUsers_userInformation_primaryEmail Expired,04/04/2017 13:18:25,02/01/2019 14:20:42 APN,,,13610875,************,,FEMALE,01/01/1970 05:31:00,000000000,INVALID_REFERENCE_VALUE,,,,CO,INPATIENT_ONLY,*****@*****.com
here userType > name not column is not in correct place and otheruserState array not come in output.
Anyone can help me?