0

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?

Ganesh Sundar
  • 311
  • 3
  • 11
  • This might seem overkill, but I would suggest possibly loading the CSV file into a database that deals with JSON. Check out Couchbase/CouchDB or MongoDB. All have query languages that let you quickly/easily query JSON structures. – ryan1234 Feb 18 '13 at 02:00

1 Answers1

0

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"]"]
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265