使用嵌套索引解析一个json文件并另存为csv [英] Parse a json file with nested indexes and save as csv

查看:120
本文介绍了使用嵌套索引解析一个json文件并另存为csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个json文件嵌套索引。有一个称为用户的索引,其具有称为lang的子索引,以及许多其他子索引。我只想提取lang字段,并将其保存为csv。对于保存为csv部分,我可以使用开源json2csv代码之一,我猜。

使用 JSON.stringify()可以帮助我解决lang 将数据转换为字符串,然后使用 match 与RegExp返回指定键/值对的数组。以下是一个示例:

  var foo = JSON.stringify({
Region:{
filterField:kw_Region,
filterValues:[
aa,
bb
]
} :{
filterField:kw_ApplicationName,
filterValues:[
aa,
bb
]
} b $ bIssueType:{
filterField:kw_IssueType,
filterValues:[
aa,
bb
] b $ b},
Outage:{
filterField:kw_Outage,
filterValues:[
aa,
bb
]
},
Priority:{
filterField:kw_Priority,
filterValues:[
aa,
bb
]
}
})。match(/(?= filterValues)[^ \]] *。/ g)

console.log(foo)// [filterValues:[aa,bb],filterValues:[aa,bb],filterValues:[aa,bb ],filterValues:[aa,bb],filterValues:[aa,bb]]


So, I have a json file with nested indexes. There's an index called "user" which has a subindex called "lang", along with a lot of other subindexes. I only want to extract the "lang" fields out and save it as csv. For the saving as csv part, I can use one of the open source "json2csv" codes, I guess. Could someone help me out with extracting the "lang" fields?

解决方案

Use JSON.stringify() to convert the data into a string, then use match with a RegExp to return an array of specified key/value pairs. Here is an example:

var foo = JSON.stringify({
"Region": {
    "filterField": "kw_Region",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"ApplicationName": {
    "filterField": "kw_ApplicationName",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"IssueType": {
    "filterField": "kw_IssueType",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"Outage": {
    "filterField": "kw_Outage",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"Priority": {
    "filterField": "kw_Priority",
    "filterValues": [
        "aa",
        "bb"
    ]
}
}).match(/(?=filterValues)[^\]]*./g)

console.log(foo) // ["filterValues":["aa","bb"]", "filterValues":["aa","bb"]", "filterValues":["aa","bb"]", "filterValues":["aa","bb"]", "filterValues":["aa","bb"]"]

这篇关于使用嵌套索引解析一个json文件并另存为csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆