<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic NewtonSoft Deserialize nested JSON Blue Prism in Digital Exchange</title>
    <link>https://community.blueprism.com/t5/Digital-Exchange/NewtonSoft-Deserialize-nested-JSON-Blue-Prism/m-p/49192#M35</link>
    <description>&lt;P&gt;I'm using Blue Prism to call the Microsoft Computer Vision API to recognize text from a PDF. &lt;/P&gt;

&lt;P&gt;After getting the JSON response, Blue Prism transforms it into a Collection (DataTable) using either the JSON Utility from Blue Prism, or the Action Provided on the Microsoft Computer Vision skill: Get Read Operation Response.&lt;/P&gt;

&lt;P&gt;Problem: Blue Prism is unable parse the JSON text into a Collection in certain cases due to a data type error.&lt;/P&gt;

&lt;P&gt;Can someone sugest a code to convert &lt;STRONG&gt;this type of JSON structure&lt;/STRONG&gt; into a DataTable which Blue Prism can read?&lt;/P&gt;

&lt;P&gt;I have attached pastebin links for the 2 JSON (Body is limited to 30000 characters, can't copy them here). &lt;/P&gt;

&lt;P&gt;READABLE in Blue Prism (contains special characters !'#$%&amp;amp;/()''):&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;&lt;A href="https://pastebin.com/QMPCiQty" rel="nofollow noreferrer"&gt;https://pastebin.com/QMPCiQty&lt;/A&gt;&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;NOT READABLE in Blue Prism:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;&lt;A href="https://pastebin.com/V3HVssQz" rel="nofollow noreferrer"&gt;https://pastebin.com/V3HVssQz&lt;/A&gt;&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Blue Prism gives the following errors: &lt;/P&gt;

&lt;P&gt;Using 'Get Read Operation Response' action: &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;Internal : Unexpected error Data Type mismatch in array: Element '3' has type instead of expected type 'System.Double'&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Using 'JSON Utility from Blue Prism': &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;Internal : Could not execute code stage because exception thrown by code stage: Data Type mismatch in array&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I tried to parse the nested JSON into a datatable by creating the public classes using the tool provided at json2csharp.com, and then point to them and write to the data table. I also tried by not creating the classes, just parse the JSON directly to the data table by pointing to the property &lt;STRONG&gt;recognitionResults&lt;/STRONG&gt;.&lt;/P&gt;

&lt;P&gt;Without classes: &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;DataSet ds = JObject.Parse(json_txt)['recognitionResults'].ToObject();&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;With classes:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;DataTable dt = (DataTable)JsonConvert.DeserializeObject(json_txt, (typeof(DataTable)));&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Classes:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;public class Word
{
    public List&amp;lt;double&amp;gt; boundingBox { get; set; }
    public string text { get; set; }
    public string confidence { get; set; }
}

public class Line
{
    public List&amp;lt;double&amp;gt; boundingBox { get; set; }
    public string text { get; set; }
    public List&amp;lt;Word&amp;gt; words { get; set; }
}

public class RecognitionResult
{
    public int page { get; set; }
    public double clockwiseOrientation { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public string unit { get; set; }
    public List&amp;lt;Line&amp;gt; lines { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public List&amp;lt;RecognitionResult&amp;gt; recognitionResults { get; set; }
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is how the &lt;STRONG&gt;nested&lt;/STRONG&gt; collection should look like in Blue Prism:
&lt;A href="https://i.stack.imgur.com/jnqVc.png" rel="nofollow noreferrer"&gt;Blue Prism Nested Response Collection&lt;/A&gt; &lt;/P&gt;

--------------------------------------------------&lt;BR /&gt;

&lt;B&gt;Disclaimer:&lt;/B&gt; This content was auto-posted from Stackoverflow. The original Stackoverflow question is here &lt;A href="https://stackoverflow.com/questions/57518439/newtonsoft-deserialize-nested-json-blue-prism"&gt;Stackoverflow Post&lt;/A&gt;, posted by &lt;A href="https://stackoverflow.com/users/11934077/david"&gt;David&lt;/A&gt;.</description>
    <pubDate>Wed, 21 Aug 2019 20:01:00 GMT</pubDate>
    <dc:creator>Digital_WorkerB</dc:creator>
    <dc:date>2019-08-21T20:01:00Z</dc:date>
    <item>
      <title>NewtonSoft Deserialize nested JSON Blue Prism</title>
      <link>https://community.blueprism.com/t5/Digital-Exchange/NewtonSoft-Deserialize-nested-JSON-Blue-Prism/m-p/49192#M35</link>
      <description>&lt;P&gt;I'm using Blue Prism to call the Microsoft Computer Vision API to recognize text from a PDF. &lt;/P&gt;

&lt;P&gt;After getting the JSON response, Blue Prism transforms it into a Collection (DataTable) using either the JSON Utility from Blue Prism, or the Action Provided on the Microsoft Computer Vision skill: Get Read Operation Response.&lt;/P&gt;

&lt;P&gt;Problem: Blue Prism is unable parse the JSON text into a Collection in certain cases due to a data type error.&lt;/P&gt;

&lt;P&gt;Can someone sugest a code to convert &lt;STRONG&gt;this type of JSON structure&lt;/STRONG&gt; into a DataTable which Blue Prism can read?&lt;/P&gt;

&lt;P&gt;I have attached pastebin links for the 2 JSON (Body is limited to 30000 characters, can't copy them here). &lt;/P&gt;

&lt;P&gt;READABLE in Blue Prism (contains special characters !'#$%&amp;amp;/()''):&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;&lt;A href="https://pastebin.com/QMPCiQty" rel="nofollow noreferrer"&gt;https://pastebin.com/QMPCiQty&lt;/A&gt;&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;NOT READABLE in Blue Prism:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;&lt;A href="https://pastebin.com/V3HVssQz" rel="nofollow noreferrer"&gt;https://pastebin.com/V3HVssQz&lt;/A&gt;&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Blue Prism gives the following errors: &lt;/P&gt;

&lt;P&gt;Using 'Get Read Operation Response' action: &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;Internal : Unexpected error Data Type mismatch in array: Element '3' has type instead of expected type 'System.Double'&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Using 'JSON Utility from Blue Prism': &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;BLOCKQUOTE&gt;
    &lt;P&gt;Internal : Could not execute code stage because exception thrown by code stage: Data Type mismatch in array&lt;/P&gt;
  &lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I tried to parse the nested JSON into a datatable by creating the public classes using the tool provided at json2csharp.com, and then point to them and write to the data table. I also tried by not creating the classes, just parse the JSON directly to the data table by pointing to the property &lt;STRONG&gt;recognitionResults&lt;/STRONG&gt;.&lt;/P&gt;

&lt;P&gt;Without classes: &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;DataSet ds = JObject.Parse(json_txt)['recognitionResults'].ToObject();&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;With classes:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;DataTable dt = (DataTable)JsonConvert.DeserializeObject(json_txt, (typeof(DataTable)));&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Classes:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;public class Word
{
    public List&amp;lt;double&amp;gt; boundingBox { get; set; }
    public string text { get; set; }
    public string confidence { get; set; }
}

public class Line
{
    public List&amp;lt;double&amp;gt; boundingBox { get; set; }
    public string text { get; set; }
    public List&amp;lt;Word&amp;gt; words { get; set; }
}

public class RecognitionResult
{
    public int page { get; set; }
    public double clockwiseOrientation { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public string unit { get; set; }
    public List&amp;lt;Line&amp;gt; lines { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public List&amp;lt;RecognitionResult&amp;gt; recognitionResults { get; set; }
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is how the &lt;STRONG&gt;nested&lt;/STRONG&gt; collection should look like in Blue Prism:
&lt;A href="https://i.stack.imgur.com/jnqVc.png" rel="nofollow noreferrer"&gt;Blue Prism Nested Response Collection&lt;/A&gt; &lt;/P&gt;

--------------------------------------------------&lt;BR /&gt;

&lt;B&gt;Disclaimer:&lt;/B&gt; This content was auto-posted from Stackoverflow. The original Stackoverflow question is here &lt;A href="https://stackoverflow.com/questions/57518439/newtonsoft-deserialize-nested-json-blue-prism"&gt;Stackoverflow Post&lt;/A&gt;, posted by &lt;A href="https://stackoverflow.com/users/11934077/david"&gt;David&lt;/A&gt;.</description>
      <pubDate>Wed, 21 Aug 2019 20:01:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Digital-Exchange/NewtonSoft-Deserialize-nested-JSON-Blue-Prism/m-p/49192#M35</guid>
      <dc:creator>Digital_WorkerB</dc:creator>
      <dc:date>2019-08-21T20:01:00Z</dc:date>
    </item>
    <item>
      <title>RE: NewtonSoft Deserialize nested JSON Blue Prism</title>
      <link>https://community.blueprism.com/t5/Digital-Exchange/NewtonSoft-Deserialize-nested-JSON-Blue-Prism/m-p/49193#M36</link>
      <description>Hi DW,&lt;BR /&gt;&lt;BR /&gt;Do you want to try the following code. Iv tried this for Microsoft NLP responses. I have included a sample JSON that works with this code.&lt;BR /&gt;You might have to make suitable adjustments.&lt;BR /&gt;&lt;BR /&gt; class ParseLuisResponse&lt;BR /&gt; {&lt;BR /&gt; //string sampleJson = "{ \"query\": \"&amp;lt;URL&amp;gt;", \"topScoringIntent\": { \"intent\": \"None\", \"score\": 0.9240461 }, \"intents\": [ { \"intent\": \"None\", \"score\": 0.9240461 }, { \"intent\": \"GetInfrastructureInfo\", \"score\": 1.37703191E-05 } ], \"entities\": [], \"sentimentAnalysis\": { \"label\": \"positive\", \"score\": 0.7532452 }}";&lt;BR /&gt;&lt;BR /&gt; public DataTable convertToDataTable(string j)&lt;BR /&gt; {&lt;BR /&gt; DataTable dt = new DataTable();&lt;BR /&gt; JToken JO = JToken.Parse(j);&lt;BR /&gt; switch (JO.Type)&lt;BR /&gt; {&lt;BR /&gt; case JTokenType.Array:&lt;BR /&gt; dt = convertJArrayToTable(j);&lt;BR /&gt; break;&lt;BR /&gt; case JTokenType.Object:&lt;BR /&gt; dt = convertJObjectToTable(j);&lt;BR /&gt; break;&lt;BR /&gt; }&lt;BR /&gt; return dt;&lt;BR /&gt; &lt;BR /&gt; }&lt;BR /&gt; public DataTable convertJArrayToTable(string j)&lt;BR /&gt; {&lt;BR /&gt; var response = JsonConvert.DeserializeObject&amp;lt;List&amp;lt;Dictionary&amp;lt;string, Object&amp;gt;&amp;gt;&amp;gt;(j);&lt;BR /&gt;&lt;BR /&gt; DataTable dt = new DataTable();&lt;BR /&gt; if (response != null)&lt;BR /&gt; {&lt;BR /&gt; dt.Columns.AddRange(response.FirstOrDefault().Select(x =&amp;gt;&lt;BR /&gt; new DataColumn&lt;BR /&gt; {&lt;BR /&gt; ColumnName = x.Key&lt;BR /&gt; }).ToArray());&lt;BR /&gt;&lt;BR /&gt; foreach (Dictionary&amp;lt;string, Object&amp;gt; dict in response)&lt;BR /&gt; {&lt;BR /&gt; DataRow dr = dt.NewRow();&lt;BR /&gt; foreach (var kv in dict)&lt;BR /&gt; {&lt;BR /&gt; dr[kv.Key] = kv.Value.ToString();&lt;BR /&gt;&lt;BR /&gt; }&lt;BR /&gt; dt.Rows.Add(dr);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; return dt;&lt;BR /&gt;&lt;BR /&gt; }&lt;BR /&gt; public DataTable convertJObjectToTable(string j)&lt;BR /&gt; {&lt;BR /&gt; var response = JsonConvert.DeserializeObject&amp;lt;Dictionary&amp;lt;string, Object&amp;gt;&amp;gt;(j);&lt;BR /&gt;&lt;BR /&gt; DataTable dt = new DataTable();&lt;BR /&gt; if (response != null)&lt;BR /&gt; {&lt;BR /&gt; dt.Columns.AddRange(response.Select(x =&amp;gt;&lt;BR /&gt; new DataColumn&lt;BR /&gt; {&lt;BR /&gt; ColumnName = x.Key&lt;BR /&gt; }).ToArray());&lt;BR /&gt;&lt;BR /&gt; DataRow dr = dt.NewRow();&lt;BR /&gt; foreach (var kv in response)&lt;BR /&gt; {&lt;BR /&gt; dr[kv.Key] = kv.Value.ToString();&lt;BR /&gt;&lt;BR /&gt; }&lt;BR /&gt; dt.Rows.Add(dr);&lt;BR /&gt; }&lt;BR /&gt; return dt;&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;------------------------------&lt;BR /&gt;Bimal Sebastian&lt;BR /&gt;Consultant&lt;BR /&gt;Blueprism&lt;BR /&gt;Asia/Kolkata&lt;BR /&gt;------------------------------&lt;BR /&gt;</description>
      <pubDate>Thu, 22 Aug 2019 05:29:00 GMT</pubDate>
      <guid>https://community.blueprism.com/t5/Digital-Exchange/NewtonSoft-Deserialize-nested-JSON-Blue-Prism/m-p/49193#M36</guid>
      <dc:creator>BimalSebastian</dc:creator>
      <dc:date>2019-08-22T05:29:00Z</dc:date>
    </item>
  </channel>
</rss>

