首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Groovy中将JSON解析为CSV

在Groovy中将JSON解析为CSV
EN

Stack Overflow用户
提问于 2020-09-04 20:29:41
回答 1查看 59关注 0票数 0

我正在尝试将json文件解析为csv。你能帮忙吗?示例json:

代码语言:javascript
复制
{
   "expand": "schema,names",
   "startAt": 0,
   "maxResults": 50,
   "total": 21,
   "issues":    [
            {
         "expand": "operations,versionedRepresentations",
         "id": "217580",
         "self": "issue/217580",
         "key": "ART-4070",
         "fields": {"summary": "#[ART] Pre.3 Verification \\"S\\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217579",
         "self": "issue/217579",
         "key": "ART-4069",
         "fields": {"summary": "Verification \\"C\\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217577",
         "self": "issue/217577",
         "key": "ART-4068",
         "fields": {"summary": "#[ART] Enum type"}
      }
   ]
}

结果csv应如下所示:

代码语言:javascript
复制
key;summary
ART-4070;#[ART] Pre.3 Verification \"S\"
ART-4069;Verification \"C\"
ART-4068;#[ART] Enum type

我尝试过这样的代码:

代码语言:javascript
复制
import groovy.json.*

def jsonSlurper = new JsonSlurper()
def json = '''
{
   "expand": "schema,names",
   "startAt": 0,
   "maxResults": 50,
   "total": 21,
   "issues":    [
            {
         "expand": "operations,versionedRepresentations",
         "id": "217580",
         "self": "issue/217580",
         "key": "ART-4070",
         "fields": {"summary": "#[ART] Pre.3 Verification \\"S\\""}
      },
           {
         "expand": "operations,versionedRepresentations",
         "id": "217579",
         "self": "issue/217579",
         "key": "ART-4069",
         "fields": {"summary": "Verification \\"C\\""}
      },
            {
         "expand": "operations,versionedRepresentations",
         "id": "217577",
         "self": "issue/217577",
         "key": "ART-4068",
         "fields": {"summary": "#[ART] Enum type"}
      }
   ]
}
'''
def obj = jsonSlurper.parse(json)

def columns = obj.issues*.keySet().flatten().unique()

// remove nulls
def encode = { e -> e == null ? '' : e  }

// Print all the column names
println columns.collect { c -> encode( c ) }.join( ';' )

// create all the rows
println obj.issues.collect { row ->
    // A row at a time
    columns.collect { colName -> encode( row[ colName ] ) }.join( ';' )
}.join( '\n' )

但结果是错误的:

扩展;id;自身;键;字段操作,versionedRepresentations;217580;issue/217580;ART-4070;[summary:#ART Pre.3验证"S"]操作,versionedRepresentations;217579;issue/217579;ART-4069;summary:Verification "C“操作,versionedRepresentations;217577;issue/217577;ART-4068;[summary:#ART枚举类型]

如何从json文件中只提取我想要的内容?我只需要两列:键、摘要和它们的值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-04 20:43:26

您只想从您的问题列表中提取特定信息,并且您需要不同的策略来提取这些信息。因此,我将使用“配置”来描述提取(参见下面的地图config )。然后代码非常接近你的原始代码(提取了一些常见的代码等)

代码语言:javascript
复制
import groovy.json.*

def config = [ // header -> extractor
    "key": { it.key },
    "summary": { it.fields.summary }
]

def encode(e) { // help with nulls; quote the separator
    (e ?: "").replaceAll(";", "\\;")  
}

def csvLine(items) { // write items as "CSV"
    println(items.collect{ encode it }.join(";"))
}

// main
def obj = new JsonSlurper().parse("data.json" as File)
csvLine(config.keySet())
obj.issues.each{ issue ->
    csvLine(config.values().collect{ f -> f issue })
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63741224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档