Qu

csv2json

Builtin index · File I/O

result = csv2json(str)

Input: str · Output: result

What it is

str (string, CSV text) is converted to JSON. Not jsonify(parse_csv(str)) — it emits a plain JSON array of one flat {"col": value, ...} object per row (the natural shape for feeding to another JSON-consuming tool), rather than jsonify's own {"type":"table", ...} envelope. json2csv accepts either shape back, since parse_json understands both.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = csv2json(str)
str stringnot documented yet
result string
Returns a string.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

t = parse_csv("name,age\nAlice,30\nBob,25\n")
print(xmlify(t))
# <table>

#   <columns>

#     <column kind="str">name</column>

#     <column kind="num">age</column>

#   </columns>

#   <row>

#     <name>Alice</name>

#     <age>30</age>

#   </row>

#   <row>

#     <name>Bob</name>

#     <age>25</age>

#   </row>

# </table>


back = parse_xml(xmlify(t))
print(back.name[0])   # Alice


j = csv2json("product,price\nWidget,9.99\n")
print(j)               # [{"product":"Widget","price":9.99}]

output
<table>
  <columns>
    <column kind="str">name</column>
    <column kind="num">age</column>
  </columns>
  <row>
    <name>Alice</name>
    <age>30</age>
  </row>
  <row>
    <name>Bob</name>
    <age>25</age>
  </row>
</table>
    ... 2 more lines

from file-io

How it works

Row objects, not the save envelope -- see do_json_rows.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.