danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
726 stars 164 forks source link

Generalized csv to json conversion #556

Open danielaparker opened 17 hours ago

danielaparker commented 17 hours ago

Suppose, we have this source (testjson_nested.json):

[
    {
        "text": "Chicago Reader", 
        "float": 1.0, 
        "datetime": "1971-01-01T04:14:00", 
        "boolean": true,
        "nested": {
          "time": "04:14:00",
          "nested": {
            "date": "1971-01-01",
            "integer": 40
          }
        }
    }, 
    {
        "text": "Chicago Sun-Times", 
        "float": 1.27, 
        "datetime": "1948-01-01T14:57:13", 
        "boolean": true,
        "nested": {
          "time": "14:57:13",
          "nested": {
            "date": "1948-01-01",
            "integer": 63
          }
        }
    }
]

And here is how Python csvkit's utility in2csv does this conversion:

(base) C:\Users\Wiluite50>in2csv testjson_nested.json text,float,datetime,boolean,nested/time,nested/nested/date,nested/nested/integer Chicago Reader,1.0,1971-01-01T04:14:00,True,4:14:00,1971-01-01,40 Chicago Sun-Times,1.27,1948-01-01T14:57:13,True,14:57:13,1948-01-01,63

I've even seen a C++ code that did it as well (except for saving field ordering, what is very important to be). But, the jsoncons seems to be perfectly designed and quick, and it would be nice to have the same thing within it.

Originally posted by @wiluite in https://github.com/danielaparker/jsoncons/discussions/555#discussioncomment-11333899