将 json 中指定节点的数据保存到 csv 文件

例题描述和简单分析

有 json 文件 json.json,如下所示:

{

    "issues":[

        {

            "key":"abc",

            "fields":{

                "issuetype":{

                    "name":"Bug",

                    "id":"1",

                    "subtask":false

                },

                "priority":{

                    "name":"Major",

                    "id":"3"

                },

                "created":"2020-5-11",

                "status":{

                    "name":"OPEN"

                }

            }

        },

        {

            "key":"def",

            "fields":{

                "issuetype":{

                    "name":"Info",

                    "id":"5",

                    "subtask":false

                },

                "priority":{

                    "name":"Minor",

                    "id":"2"

                },

                "created":"2020-5-8",

                "status":{

                    "name":"DONE"

                }

            }

        }

    ]

}

需要从 json 各层取出部分节点,形成 csv,结果如下:

abc,Bug,Major,2020-5-11,OPEN

def,Info,Minor,2020-5-8,DONE

解法及简要说明

在集算器中编写脚本 p1.dfx,如下所示:


A

1

=json(file("json.json").read()).issues.new(key,fields.issuetype.name,fields.priority.name,fields.created,fields.status.name)

2

=file("result.csv").export@c(A1)

简要说明:

A1   json文件读成串,用 json 函数解析成多层序表,按题目要求取出对应的部分节点

A2  结果导出至 result.csv

问答搜集

https://stackoverflow.com/questions/61753408/how-to-select-fields-in-different-levels-of-a-jsonfile-with-jsonpath