Convert JSON to CSV with jq

懵懂的女人 提交于 2021-02-19 05:50:08

问题


I'm trying to extract the sids, ll, state, name, smry values in my JSON file using jq and export to a csv.

JSON File (out.json):

{
    "data": [
        {
            "meta": {
                "uid": 74529,
                "ll": [
                    -66.9333,
                    47.0667
                ],
                "sids": [
                    "CA008102500 6"
                ],
                "state": "NB",
                "elev": 1250,
                "name": "LONG LAKE"
            },
            "smry": [
                [
                    "42",
                    "1955-02-23"
                ]
            ]
        },
        {
            "meta": {
                "uid": 74534,
                "ll": [
                    -67.2333,
                    45.9667
                ],
                "sids": [
                    "CA008103425 6"
                ],
                "state": "NB",
                "elev": 150.9,
                "name": "NACKAWIC"
            },
            "smry": [
                [
                    "40",
                    "1969-02-23"
                ]
            ]
        },
        {
            "meta": {
                "uid": 74549,
                "ll": [
                    -67.4667,
                    47.4667
                ],
                "sids": [
                    "CA008104933 6"
                ],
                "state": "NB",
                "elev": 794,
                "name": "ST QUENTIN"
            },
            "smry": [
                [
                    "M",
                    "M"
                ]
            ]
        },
        {
            "meta": {
                "uid": 74550,
                "ll": [
                    -67.2667,
                    45.1833
                ],
                "sids": [
                    "CA008104936 6"
                ],
                "state": "NB",
                "elev": 36.1,
                "name": "ST STEPHEN"
            },
            "smry": [
                [
                    "48",
                    "1900-02-23"
                ]
            ]
        },
        {
            "meta": {
                "uid": 74554,
                "ll": [
                    -67.25,
                    47.2667
                ],
                "sids": [
                    "CA008105000 6"
                ],
                "state": "NB",
                "elev": 915.4,
                "name": "SISSON DAM"
            },
            "smry": [
                [
                    "35",
                    "1955-02-23"
                ]
            ]
        }
    ]
}

Terminal Code:

jq '.data | [ {sids, ll, state, name, smry} ]' out.json

I am getting the following errors:

assertion "cb == jq_util_input_next_input_cb" failed: file "/usr/src/ports/jq/jq-1.5-3.x86_64/src/jq-1.5/util.c", line 371, function: jq_util_input_get_position
Aborted (core dumped)

Example Expected Output:

sids, ll, state, name, smry
CA008102500, -66.9333, 47.0667, NB, LONG LAKE, 42,1955-02-23
CA008103425, -67.2333, 45.9667, NB, NACKAWIC, 35,1955-02-23

What am I doing wrong?


回答1:


It's a bit more complex because you need to flatten sids, ll and smry before you can flatten the whole record. I recommend to create a jq file:

foo.jq:

.data[]|{
    "sids":(.meta.sids[0]|split(" ")[0]),
    "ll":(.meta.ll|map(tostring)|join(",")),
    "state":.meta.state,
    "name":.meta.name,
    "smry":(.smry[]|join(","))
}|join(",")
# or, for robust csv output
# } | @csv 

And then call:

jq -rf foo.jq file.json

Output:

CA008102500,-66.9333,47.0667,NB,LONG LAKE,42,1955-02-23
CA008103425,-67.2333,45.9667,NB,NACKAWIC,40,1969-02-23
CA008104933,-67.4667,47.4667,NB,ST QUENTIN,M,M
CA008104936,-67.2667,45.1833,NB,ST STEPHEN,48,1900-02-23
CA008105000,-67.25,47.2667,NB,SISSON DAM,35,1955-02-23


来源:https://stackoverflow.com/questions/42414365/convert-json-to-csv-with-jq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!