Guest User

Untitled

a guest
May 16th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. # Introduction
  2. This script converts a CSV file into a custom json format
  3.  
  4.  
  5. ### Using the Tool
  6. To use the script, import the function in the following format:
  7.  
  8. ```
  9. $ csv_to_json(file_path, save_path, json_structure, indent, delimiter)
  10. ```
  11.  
  12. - **file_path**: this is the path to your csv file
  13. - **save_path**: this is the new file path to save (xx.json)
  14. - **json_structure**: this is used to specify how you want the json file to be formatted. For more, read the next section.
  15. - **indent**: The indentation for json file. By default, it indents json file by 4 spaces for easy read. If you specify None, it will not indent anything.
  16. - **delimiter**: the delimiter of how your data columns are separated. By default, it assumes csv file uses "," as delimiter.
  17.  
  18. ### Json Structure Format
  19.  
  20. **You can find examples of these in the later half of this section**
  21.  
  22. If you pass `json_structure = 'column'`, it dumps a json file with column labels (assumption: first row are labels) as key, and column data as values.
  23.  
  24. If you pass `json_structure = 'row: primary_key_label'`, it dumps a json file with the value of `primary_key_label` column as the key, and the values being the mapping of the colum name to that data point for that row.
  25.  
  26. If you want to customize the json structure, pass a dictionary in the following format, depending on your goal:
  27.  
  28. **Note: the `key_label` and `data_column_name` has to match exactly with the names in csv file!**
  29.  
  30. - You want to format each ROW of the csv file
  31.  
  32. ```
  33. {
  34. "key_label": [
  35. "data_column_name_1",
  36. {
  37. "sub_key_label": "data_column_name_2",
  38. ...
  39. },
  40. ...
  41. ],
  42. }
  43. ```
  44.  
  45. In the above format, it uses the value at `key_label` column of each row as the json key for that row, and recursively format the sub_structure of the json file, where values are value at `data_column_name_x` column of that row.
  46.  
  47. - You want to format each COLUMN of the csv file
  48.  
  49. ```
  50. {
  51. "key_label_1": [
  52. "data_column_name_1",
  53. {
  54. "sub_key_label": "data_column_name_2",
  55. ...
  56. },
  57. ...
  58. ],
  59. "key_label_2": [
  60. "data_column_name_3",
  61. {
  62. "sub_key_label": "data_column_name_2",
  63. ...
  64. },
  65. ...
  66. ],
  67. }
  68. ```
  69.  
  70. In the above format, it uses `key_label_x` LITERALLY as the json key, and recursively format the sub_structure of the json file, where values are ALL the column data for `data_column_name_x` column.
  71.  
  72. #### Examples
  73.  
  74. Say, you have a csv file like so:
  75.  
  76. First Name | Last Name | Email Address| Phone Number
  77. --- | --- | --- | ---
  78. Alex | Wong | example@gmail.com | 111-222-333
  79. Alice | Andressen | example@domain.com | 444-555-666
  80.  
  81. * Using `json structure = 'column'`:
  82.  
  83. ```
  84. {
  85. "First Name": [
  86. "Alex",
  87. "Alice"
  88. ],
  89. "Last Name": [
  90. "Wong",
  91. "Andressen"
  92. ],
  93. "Email Address": [
  94. "example@gmail.com",
  95. "example@domain.com"
  96. ],
  97. "Phone Number": [
  98. "111-222-333",
  99. "444-555-666"
  100. ]
  101. }
  102. ```
  103.  
  104.  
  105. * Using `json structure = 'row: Email Address'`:
  106.  
  107. ```
  108. {
  109. "111-222-333": {
  110. "First Name": "Alex",
  111. "Last Name": "Wong",
  112. "Email Address": "example@gmail.com",
  113. "Phone Number": "111-222-333"
  114. },
  115. "444-555-666": {
  116. "First Name": "Alice",
  117. "Last Name": "Andressen",
  118. "Email Address": "example@domain.com",
  119. "Phone Number": "444-555-666"
  120. }
  121. }
  122. ```
  123.  
  124. * Using json structure 1:
  125.  
  126. ```
  127. {
  128. "First Name": {
  129. "Last Name": "Last Name",
  130. "Contact Information": {
  131. "Email": "Email Address",
  132. "Phone": "Phone Number"
  133. }
  134. }
  135. }
  136. ```
  137.  
  138. will return the following json file:
  139.  
  140. ```
  141. {
  142. "Alex": {
  143. "Last Name": "Wong",
  144. "Contact Information": {
  145. "Email": "example@gmail.com",
  146. "Phone": "111-222-333"
  147. }
  148. },
  149. "Alice": {
  150. "Last Name": "Andressen",
  151. "Contact Information": {
  152. "Email": "example@domain.com",
  153. "Phone": "444-555-666"
  154. }
  155. }
  156. }
  157. ```
  158.  
  159. * Using json structure 2:
  160.  
  161. ```
  162. {
  163. "Name": {
  164. "First Names": "First Name",
  165. "Last Names": "Last Name"
  166. },
  167. "Email List": "Email Address",
  168. "Phones": "Phone Number"
  169. }
  170. ```
  171.  
  172. will return the following json file:
  173.  
  174. ```
  175. {
  176. "Name": {
  177. "First Names": [
  178. "Alex",
  179. "Alice"
  180. ],
  181. "Last Names": [
  182. "Wong",
  183. "Andressen"
  184. ]
  185. },
  186. "Email List": [
  187. "example@gmail.com",
  188. "example@domain.com"
  189. ],
  190. "Phones": [
  191. "111-222-333",
  192. "444-555-666"
  193. ]
  194. }
  195. ```
  196.  
  197.  
  198. # Software
  199. You need Python (2/3).
Add Comment
Please, Sign In to add comment