Comments (3)
Hi @jimthedev, thanks!
I can definitely see the usefulness of supporting keys nested in arrays, which it appears the current keys
option doesn't support based on my testing. Unfortunately I won't have time to look into this for a little bit (because of grad school exams coming up), but it would be a nice addition.
Just for future reference, here's what I tried with the current code:
var converter = require('json-2-csv');
var options = {
keys : ['Make', 'Model', 'Year', 'Features.name']
};
var documents = [
{
Make: 'Nissan',
Model: ' Murano ', // Note: This value has additional padding which can be trimmed
Year: '2013',
Features: [{ name: 'AC', value: 1}, {name: 'DVD', value: 2}]
}
];
var json2csvCallback = function (err, csv) {
if (err) throw err;
console.log(csv);
};
converter.json2csv(documents, json2csvCallback, options);
from json-2-csv.
Cool. That looks similar to what I have been testing (which currently doesn't work). If I get to it I'll do a PR. Thanks for confirming.
from json-2-csv.
As of 3.2.0, this is now supported! So for the following JSON documents, you can specify {keys: ['features.name']}
in your options and it will pull the correct values:
[
{
"name": "list",
"features": [
{
"name": "modules"
},
{
"pros": "efficiency"
},
{
"cons": ["cost", "time"]
}
]
},
{
"name": "module",
"features": [
{
"name": "testing"
}
],
"downloads": "5k/m"
}
]
However, if you want to take full advantage of the latest changes and have the most user-friendly CSV, then I'd also recommend specifying expandArrayObjects: true
in your options object. That will tell the module that it should also parse array objects so that they're a bit more readable. The generated CSV when using this option will look like this for the above pasted JSON:
features.name
modules
testing
Thanks for reporting this and for your patience!
from json-2-csv.
Related Issues (20)
- Headerless CSV HOT 3
- Incorrect parsing of wrapped values HOT 5
- Custom sorting of headers HOT 2
- How to properly change mongoDB timestamp to proper date? HOT 1
- TypeError: Cannot convert undefined or null to object HOT 1
- Private Vulnerability Disclosure HOT 1
- csv2json Allow CSV Files Without Headers HOT 3
- NestedDotKeys with false and 0 are returned as undefined HOT 4
- prependHeader: false, is not working HOT 2
- Paginated conversion ? HOT 1
- "expandArrayObjects" option not behaving as expected HOT 3
- sync version HOT 2
- Incorrect parsing of wrapped values when the new line is right after the starting quote HOT 1
- Ignoring an entire array with `excludeKeys` and `expandArrayObjects` HOT 2
- No possibility to use unescaped dots in headers HOT 11
- Feature Request: expandNestedObjects functionality with keys HOT 10
- Providing option.keys[] field with RegExp throws kp.slice() is not a function HOT 2
- Add support for RegExps in excludeKeys
- Field in CSV it turned into an object unexpectedly. HOT 2
- why json to csv adds `\` before `.` in headers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-2-csv.