Select Specified JSON Nodes to Store in a CSV File
Problem description & analysis
Below is the content of JSON file 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"
}
}
}
]
}
We are trying to retrieve certain nodes in different levels of the JSON file to generate a CSV file, as shown below:
abc,Bug,Major,2020-5-11,OPEN
def,Info,Minor,2020-5-8,DONE
Solution
Write the following script p1.dfx in esProc:
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) |
Explanation:
A1 Read the JSON file as a string, parse it as a multilevel table sequence using json() function, and get desired nodes.
A2 Export retrieved nodes to result.csv.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL