{"id":14044,"date":"2022-10-06T08:17:30","date_gmt":"2022-10-06T02:47:30","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/"},"modified":"2022-10-06T08:17:30","modified_gmt":"2022-10-06T02:47:30","slug":"solved-json-data-not-converting-to-list","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/","title":{"rendered":"[Solved] Json data not converting to List"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-43150098\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"43150098\" data-parentid=\"43145603\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Firstly, both your input and output JSON are syntactically invalid: they are missing outer braces <code>{<\/code> and <code>}<\/code>.  For the remainder of this answer, I&#8217;m going to assume this is a typo in the question.<\/p>\n<p>Assuming you have not done so already, you could install json.net as shown here and then use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.newtonsoft.com\/json\/help\/html\/LINQtoJSON.htm\">LINQ to JSON<\/a> to load and modify your JSON.  Using this approach avoids the need to define c# types that perfectly match your JSON.<\/p>\n<p>Your input JSON has two problems:<\/p>\n<ul>\n<li>\n<p>The tokens <code>\"channels.heart-rate.events\"<\/code> and <code>\"channels.location.events\"<\/code> are arrays of objects for which <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.newtonsoft.com\/json\/help\/html\/SerializeTypeNameHandling.htm\">Json.NET type information<\/a> has been included.  (It is clear from the presence of the <code>\"$type\"<\/code> property that the JSON was originally generated using Json.NET)<\/p>\n<p>What you want instead is for these arrays to be reformatted into a single object that contains the item type, an array of item property names, and an array of array of item property values.<\/p>\n<\/li>\n<li>\n<p>The type information has been formatted using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.newtonsoft.com\/json\/help\/html\/P_Newtonsoft_Json_JsonSerializer_TypeNameAssemblyFormat.htm\"><code>TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple<\/code><\/a>.  You want to add assembly information, converting to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/tt9xha1h\"><code>FormatterAssemblyStyle.Full<\/code><\/a> format.<\/p>\n<\/li>\n<\/ul>\n<p>Both of these can be corrected using LINQ-to-JSON.  Say you have two streams, <code>Stream inputStream<\/code> and <code>Stream outputStream<\/code> corresponding to a stream with the JSON to be fixed and a stream in which to store the fixed JSON.  Then, introduce the following utility methods:<\/p>\n<pre><code>public static class JsonExtensions\n{\n    const string JsonTypeName = @\"$type\";\n    const string JsonValuesName = @\"$values\";\n\n    public static void ReformatCollections(Stream inputStream, Stream outputStream, IEnumerable&lt;string&gt; paths, Func&lt;string, string&gt; typeNameMapper, Formatting formatting)\n    {\n        var root = JToken.Load(new JsonTextReader(new StreamReader(inputStream)) { DateParseHandling = DateParseHandling.None });\n        root = ReformatCollections(root, paths, typeNameMapper);\n\n        var writer = new StreamWriter(outputStream);\n        var jsonWriter = new JsonTextWriter(writer) { Formatting = formatting };\n\n        root.WriteTo(jsonWriter);\n        jsonWriter.Flush();\n        writer.Flush();\n    }\n\n    public static JToken ReformatCollections(JToken root, IEnumerable&lt;string&gt; paths, Func&lt;string, string&gt; typeNameMapper)\n    {\n        foreach (var path in paths)\n        {\n            var token = root.SelectToken(path);\n            var newToken = token.ReformatCollection(typeNameMapper);\n            if (root == token)\n                root = newToken;\n        }\n\n        return root;\n    }\n\n    public static JToken ReformatCollection(this JToken value, Func&lt;string, string&gt; typeNameMapper)\n    {\n        if (value == null || value.Type == JTokenType.Null)\n            return value;\n\n        var array = value as JArray;\n        if (array == null)\n            array = value[JsonValuesName] as JArray;\n        if (array == null)\n            return value;\n\n        \/\/ Extract the item $type and ordered set of properties.\n        string type = null;\n        var properties = new Dictionary&lt;string, int&gt;();\n        foreach (var item in array)\n        {\n            if (item.Type == JTokenType.Null)\n                continue;\n            var obj = item as JObject;\n            if (obj == null)\n                throw new JsonSerializationException(string.Format(\"Item \\\"{0}\\\" was not a JObject\", obj.ToString(Formatting.None)));\n            var objType = (string)obj[JsonTypeName];\n            if (objType != null &amp;&amp; type == null)\n                type = objType;\n            else if (objType != null &amp;&amp; type != null)\n            {\n                if (type != objType)\n                    throw new JsonSerializationException(\"Too many item types.\");\n            }\n            foreach (var property in obj.Properties().Where(p =&gt; p.Name != JsonTypeName))\n            {\n                if (!properties.ContainsKey(property.Name))\n                    properties.Add(property.Name, properties.Count);\n            }\n        }\n        var propertyList = properties.OrderBy(p =&gt; p.Value).Select(p =&gt; p.Key).ToArray();\n        var newValue = new JObject();\n        if (type != null)\n            newValue[\"type\"] = JToken.FromObject(typeNameMapper(type));\n        newValue[\"structure\"] = JToken.FromObject(propertyList);\n        newValue[\"list\"] = JToken.FromObject(array\n            .Select(o =&gt; (o.Type == JTokenType.Null ? o : propertyList.Where(p =&gt; o[p] != null).Select(p =&gt; o[p]))));\n        if (value.Parent != null)\n            value.Replace(newValue);\n        return newValue;\n    }\n}\n<\/code><\/pre>\n<p>Then, at the top level of your console method, you can fix your JSON as follows:<\/p>\n<pre><code>    Func&lt;string, string&gt; typeNameMapper = (t) =&gt;\n        {\n            if (!t.EndsWith(\", Version=1.2.7.0, Culture=neutral, PublicKeyToken=null\"))\n                t = t + \", Version=1.2.7.0, Culture=neutral, PublicKeyToken=null\";\n            return t;\n        };\n    var paths = new[]\n        {\n            \"channels.heart-rate.events\",\n            \"channels.location.events\"\n        };\n    JsonExtensions.ReformatCollections(inputStream, outputStream, paths, typeNameMapper, Formatting.Indented);\n<\/code><\/pre>\n<p>Sample <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dotnetfiddle.net\/iferpX\">fiddle<\/a>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Json data not converting to List <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Firstly, both your input and output JSON are syntactically invalid: they are missing outer braces { and }. For the remainder of this answer, I&#8217;m going to assume this is a typo in the question. Assuming you have not done so already, you could install json.net as shown here and then use LINQ to &#8230; <a title=\"[Solved] Json data not converting to List\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\" aria-label=\"More on [Solved] Json data not converting to List\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,356,935],"class_list":["post-14044","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-json","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Json data not converting to List - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Json data not converting to List - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Firstly, both your input and output JSON are syntactically invalid: they are missing outer braces { and }. For the remainder of this answer, I&#8217;m going to assume this is a typo in the question. Assuming you have not done so already, you could install json.net as shown here and then use LINQ to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-06T02:47:30+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Json data not converting to List\",\"datePublished\":\"2022-10-06T02:47:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\"},\"wordCount\":241,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"json\",\"serialization\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\",\"name\":\"[Solved] Json data not converting to List - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-06T02:47:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Json data not converting to List\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Json data not converting to List - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Json data not converting to List - JassWeb","og_description":"[ad_1] Firstly, both your input and output JSON are syntactically invalid: they are missing outer braces { and }. For the remainder of this answer, I&#8217;m going to assume this is a typo in the question. Assuming you have not done so already, you could install json.net as shown here and then use LINQ to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/","og_site_name":"JassWeb","article_published_time":"2022-10-06T02:47:30+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Json data not converting to List","datePublished":"2022-10-06T02:47:30+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/"},"wordCount":241,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","json","serialization"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/","url":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/","name":"[Solved] Json data not converting to List - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-06T02:47:30+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-json-data-not-converting-to-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Json data not converting to List"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=14044"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14044\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=14044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=14044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=14044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}