Weyard Wiz

Weyard Wiz

  • NA
  • 10
  • 606

How to get values from elements in JSON without indexing?

Oct 23 2020 6:42 PM
I have the following code that extracts json elements values and outputs to csv:
  1. public static void Json_to_Csv(string jsonInputFile, string csvFile) {  
  2.   using(var p = new ChoJSONReader(jsonInputFile).WithJSONPath("$..readResults")) //   
  3.   "readResults": [{  
  4.     using(var w = new ChoCSVWriter(csvFile).WithFirstLineHeader()) {  
  5.       w.Write(p.Select(r1 =>{  
  6.         var lines = (dynamic[]) r1.lines;  
  7.         return new {  
  8.           FileName = jsonInputFile,  
  9.           Page = r1.page,  
  10.           PracticeName = lines[2].text,  
  11.           OwnerFullName = lines[4].text,  
  12.           OwnerEmail = lines[6].text,  
  13.         };  
  14.       }  
  15.     }  
  16.   }  
  17. }  
csv output:
 
File Name,Page,Practice Name,Owner Full Name,Owner Email
file1.json,1,Some Practice Name,Bob Lee,[email protected]
 
Currently there is no other contextual information on each item to reference them so the only way is by indexing, e.g. lines[2]
 
This works for now but I may have other JSON files that have an extra field, therefore the values pulled will be wrong.
 
In order to address this scenario, how can i pull the values contextually instead of indexing the lines?
 
Ive tried PracticeName = lines["Practice Name"].text,, but i get Cannot implicitly convert type string to int error
 
file1.json sample:
  1. {  
  2.    "status":"succeeded",  
  3.    "createdDateTime":"2020-10-22T19:35:35Z",  
  4.    "lastUpdatedDateTime":"2020-10-22T19:35:36Z",  
  5.    "analyzeResult":{  
  6.       "version":"3.0.0",  
  7.       "readResults":[  
  8.          {  
  9.             "page":1,  
  10.             "angle":0,  
  11.             "width":8.5,  
  12.             "height":11,  
  13.             "unit":"inch",  
  14.             "lines":[  
  15.                {  
  16.                   "boundingBox":[  
  17.                      0.5016,  
  18.                      1.9141,  
  19.                      2.5726,  
  20.                      1.9141,  
  21.                      2.5726,  
  22.                      2.0741,  
  23.                      0.5016,  
  24.                      2.0741  
  25.                   ],  
  26.                   "text":"Account Information",  
  27.                   "words":[  
  28.                      {  
  29.                         "boundingBox":[  
  30.                            0.5016,  
  31.                            1.9345,  
  32.                            1.3399,  
  33.                            1.9345,  
  34.                            1.3399,  
  35.                            2.0741,  
  36.                            0.5016,  
  37.                            2.0741  
  38.                         ],  
  39.                         "text":"Account",  
  40.                         "confidence":1  
  41.                      },  
  42.                      {  
  43.                         "boundingBox":[  
  44.                            1.3974,  
  45.                            1.9141,  
  46.                            2.5726,  
  47.                            1.9141,  
  48.                            2.5726,  
  49.                            2.0741,  
  50.                            1.3974,  
  51.                            2.0741  
  52.                         ],  
  53.                         "text":"Information",  
  54.                         "confidence":1  
  55.                      }  
  56.                   ]  
  57.                },  
  58.                {  
  59.                   "boundingBox":[  
  60.                      1.7716,  
  61.                      2.4855,  
  62.                      2.8793,  
  63.                      2.4855,  
  64.                      2.8793,  
  65.                      2.6051,  
  66.                      1.7716,  
  67.                      2.6051  
  68.                   ],  
  69.                   "text":"Practice Name",  
  70.                   "words":[  
  71.                      {  
  72.                         "boundingBox":[  
  73.                            1.7716,  
  74.                            2.4855,  
  75.                            2.3803,  
  76.                            2.4855,  
  77.                            2.3803,  
  78.                            2.6051,  
  79.                            1.7716,  
  80.                            2.6051  
  81.                         ],  
  82.                         "text":"Practice",  
  83.                         "confidence":1  
  84.                      },  
  85.                      {  
  86.                         "boundingBox":[  
  87.                            2.4362,  
  88.                            2.4948,  
  89.                            2.8793,  
  90.                            2.4948,  
  91.                            2.8793,  
  92.                            2.6051,  
  93.                            2.4362,  
  94.                            2.6051  
  95.                         ],  
  96.                         "text":"Name",  
  97.                         "confidence":1  
  98.                      }  
  99.                   ]  
  100.                },  
  101.                {  
  102.                   "boundingBox":[  
  103.                      2.9993,  
  104.                      2.5257,  
  105.                      4.7148,  
  106.                      2.5257,  
  107.                      4.7148,  
  108.                      2.714,  
  109.                      2.9993,  
  110.                      2.714  
  111.                   ],  
  112.                   "text":"Some Practice Name",  
  113.                   "words":[  
  114.                      {  
  115.                         "boundingBox":[  
  116.                            3.0072,  
  117.                            2.5385,  
  118.                            3.6546,  
  119.                            2.5284,  
  120.                            3.6516,  
  121.                            2.7131,  
  122.                            3.0105,  
  123.                            2.712  
  124.                         ],  
  125.                         "text":"Some",  
  126.                         "confidence":0.984  
  127.                      },  
  128.                      {  
  129.                         "boundingBox":[  
  130.                            3.6887,  
  131.                            2.5281,  
  132.                            4.2112,  
  133.                            2.5262,  
  134.                            4.2028,  
  135.                            2.7159,  
  136.                            3.6854,  
  137.                            2.7132  
  138.                         ],  
  139.                         "text":"Parctice",  
  140.                         "confidence":0.986  
  141.                      },  
  142.                      {  
  143.                         "boundingBox":[  
  144.                            4.2453,  
  145.                            2.5263,  
  146.                            4.7223,  
  147.                            2.5297,  
  148.                            4.7091,  
  149.                            2.72,  
  150.                            4.2366,  
  151.                            2.7161  
  152.                         ],  
  153.                         "text":"Name",  
  154.                         "confidence":0.986  
  155.                      }  
  156.                   ]  
  157.                },  
  158.                {  
  159.                   "boundingBox":[  
  160.                      1.6116,  
  161.                      2.9999,  
  162.                      2.8816,  
  163.                      2.9999,  
  164.                      2.8816,  
  165.                      3.1158,  
  166.                      1.6116,  
  167.                      3.1158  
  168.                   ],  
  169.                   "text":"Owner Full Name",  
  170.                   "words":[  
  171.                      {  
  172.                         "boundingBox":[  
  173.                            1.6116,  
  174.                            3.0039,  
  175.                            2.1026,  
  176.                            3.0039,  
  177.                            2.1026,  
  178.                            3.1157,  
  179.                            1.6116,  
  180.                            3.1157  
  181.                         ],  
  182.                         "text":"Owner",  
  183.                         "confidence":1  
  184.                      },  
  185.                      {  
  186.                         "boundingBox":[  
  187.                            2.1541,  
  188.                            2.9999,  
  189.                            2.3784,  
  190.                            2.9999,  
  191.                            2.3784,  
  192.                            3.1158,  
  193.                            2.1541,  
  194.                            3.1158  
  195.                         ],  
  196.                         "text":"Full",  
  197.                         "confidence":1  
  198.                      },  
  199.                      {  
  200.                         "boundingBox":[  
  201.                            2.4384,  
  202.                            3.0052,  
  203.                            2.8816,  
  204.                            3.0052,  
  205.                            2.8816,  
  206.                            3.1155,  
  207.                            2.4384,  
  208.                            3.1155  
  209.                         ],  
  210.                         "text":"Name",  
  211.                         "confidence":1  
  212.                      }  
  213.                   ]  
  214.                },  
  215.                {  
  216.                   "boundingBox":[  
  217.                      2.9993,  
  218.                      3.0242,  
  219.                      3.6966,  
  220.                      3.0242,  
  221.                      3.6966,  
  222.                      3.2125,  
  223.                      2.9993,  
  224.                      3.2014  
  225.                   ],  
  226.                   "text":"Bob Lee",  
  227.                   "words":[  
  228.                      {  
  229.                         "boundingBox":[  
  230.                            3.0063,  
  231.                            3.0303,  
  232.                            3.3439,  
  233.                            3.0349,  
  234.                            3.3461,  
  235.                            3.2125,  
  236.                            3.007,  
  237.                            3.2081  
  238.                         ],  
  239.                         "text":"Bob",  
  240.                         "confidence":0.987  
  241.                      },  
  242.                      {  
  243.                         "boundingBox":[  
  244.                            3.3788,  
  245.                            3.0349,  
  246.                            3.6931,  
  247.                            3.0326,  
  248.                            3.697,  
  249.                            3.2121,  
  250.                            3.3813,  
  251.                            3.2125  
  252.                         ],  
  253.                         "text":"Lee",  
  254.                         "confidence":0.983  
  255.                      }  
  256.                   ]  
  257.                },  
  258.                {  
  259.                   "boundingBox":[  
  260.                      1.945,  
  261.                      3.5063,  
  262.                      2.8748,  
  263.                      3.5063,  
  264.                      2.8748,  
  265.                      3.6261,  
  266.                      1.945,  
  267.                      3.6261  
  268.                   ],  
  269.                   "text":"Owner Email",  
  270.                   "words":[  
  271.                      {  
  272.                         "boundingBox":[  
  273.                            1.945,  
  274.                            3.5143,  
  275.                            2.4359,  
  276.                            3.5143,  
  277.                            2.4359,  
  278.                            3.6261,  
  279.                            1.945,  
  280.                            3.6261  
  281.                         ],  
  282.                         "text":"Owner",  
  283.                         "confidence":1  
  284.                      },  
  285.                      {  
  286.                         "boundingBox":[  
  287.                            2.4874,  
  288.                            3.5063,  
  289.                            2.8748,  
  290.                            3.5063,  
  291.                            2.8748,  
  292.                            3.6259,  
  293.                            2.4874,  
  294.                            3.6259  
  295.                         ],  
  296.                         "text":"Email",  
  297.                         "confidence":1  
  298.                      }  
  299.                   ]  
  300.                },  
  301.                {  
  302.                   "boundingBox":[  
  303.                      3.0104,  
  304.                      3.5005,  
  305.                      4.6042,  
  306.                      3.5005,  
  307.                      4.6042,  
  308.                      3.6888,  
  309.                      3.0104,  
  310.                      3.6777  
  311.                   ],  
  312.                   "text":"[email protected]",  
  313.                   "words":[  
  314.                      {  
  315.                         "boundingBox":[  
  316.                            3.0212,  
  317.                            3.5047,  
  318.                            4.5837,  
  319.                            3.5039,  
  320.                            4.5769,  
  321.                            3.6886,  
  322.                            3.0129,  
  323.                            3.6787  
  324.                         ],  
  325.                         "text":"[email protected]",  
  326.                         "confidence":0.951  
  327.                      }  
  328.                   ]  
  329.                }  
  330.             ]  
  331.          }  
  332.       ]  
  333.    }  
  334. }

Answers (2)