Socket
Socket
Sign inDemoInstall

json-2-csv

Package Overview
Dependencies
Maintainers
1
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-2-csv - npm Package Compare versions

Comparing version 5.2.0 to 5.3.0

38

lib/json2csv.js

@@ -174,2 +174,18 @@ 'use strict';

}
function extractWildcardMatchKeys() {
if (!options.keys)
return [];
return options.keys.flatMap(item => {
if (typeof item === 'string') {
// Exclude plain strings that were passed in options.keys
return [];
}
else if (item?.wildcardMatch) {
// Return "field" value for objects with wildcardMatch: true
return item.field;
}
// Exclude other objects
return [];
});
}
/**

@@ -180,12 +196,28 @@ * Retrieve the headings for all documents and return it.

function retrieveHeaderFields(data) {
const wildcardMatchKeys = extractWildcardMatchKeys();
const keyStrings = convertKeysToHeaderFields();
const fieldNames = getFieldNameList(data);
const processed = processSchemas(fieldNames);
if (options.keys) {
options.keys = keyStrings;
const matchedKeys = keyStrings.flatMap((userProvidedKey) => {
// If this is not a wildcard matched key, then just return and include it in the resulting key list
if (!wildcardMatchKeys.includes(userProvidedKey)) {
return userProvidedKey;
}
// Otherwise, identify all detected keys that match with the provided wildcard key:
const matches = [];
const regex = new RegExp(`^${userProvidedKey}`);
for (const detectedKey of processed) {
if (userProvidedKey === detectedKey || detectedKey.match(regex)) {
matches.push(detectedKey);
}
}
return matches;
});
if (!options.unwindArrays) {
const filtered = filterExcludedKeys(keyStrings);
const filtered = filterExcludedKeys(matchedKeys);
return sortHeaderFields(filtered);
}
}
const fieldNames = getFieldNameList(data);
const processed = processSchemas(fieldNames);
const filtered = filterExcludedKeys(processed);

@@ -192,0 +224,0 @@ return sortHeaderFields(filtered);

@@ -12,2 +12,3 @@ interface DelimiterOptions {

title?: string;
wildcardMatch?: boolean;
})[];

@@ -14,0 +15,0 @@ interface SharedConverterOptions {

2

package.json

@@ -8,3 +8,3 @@ {

"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
"version": "5.2.0",
"version": "5.3.0",
"homepage": "https://mrodrig.github.io/json-2-csv",

@@ -11,0 +11,0 @@ "repository": {

@@ -131,5 +131,15 @@ # json-2-csv

* Default: These will be auto-detected from your data by default.
* Keys can either be specified as a String representing the key path that should be converted, or as an Object with the `field` property specifying the path. When specifying keys as an Object, you can also optionally specify a `title` which will be used for that column in the header. The list specified can contain a combination of Objects and Strings.
* `[ 'key1', 'key2', ... ]`
* `[ { field: 'key1', title: 'Key 1' }, { field: 'key2' }, 'key3', ... ]`
* Keys can either be specified as a String representing the key path that should be converted, or as an Object of the following format:
```javascript
{
"field": "string", // required
"title": "string", // optional
"wildcardMatch": false, // optional - default: false
}
```
* When specifying keys as an Object, the `field` property specifies the key path, while `title` specifies a more human readable field heading. Additionally, the `wildcardMatch` option allows you to optionally specify that all auto-detected fields with the specified field prefix should be included in the CSV. The list specified can contain a combination of Objects and Strings.
* Examples:
* `[ 'key1', 'key2', ... ]`
* `[ 'key1', { field: 'key2', wildcardMatch: true }]`
* `[ { field: 'key1', title: 'Key 1' }, { field: 'key2' }, 'key3', ... ]`
* Key Paths - If you are converting a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']

@@ -136,0 +146,0 @@ * `parseValue` - Function - Specify how values should be converted into CSV format. This function is provided a single field value at a time and must return a `String`. The built-in parsing method is provided as the second argument for cases where default parsing is preferred.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc