Convert a json object to a comma seperated value (CSV) string

How to convert a json object to a comma seperated value string

Updated:
Utility FunctionString Manipulation
function
function jsonToCsv(items: Object[]): string
jsonToCsv
(
items: Object[]
items
:
interface Object

Provides functionality common to all JavaScript objects.

Object
[]): string {
if (
items: Object[]
items
.
Array<Object>.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

length
== 0 || !
items: Object[]
items
) return "";
const
const header: readonly (keyof Object)[]
header
=
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.keys(o: {}): string[] (+1 overload)

Returns the names of the enumerable string properties and methods of an object.

@paramo Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

keys
(
items: Object[]
items
[0]) as readonly (keyof (typeof
items: Object[]
items
)[number])[];
const
const headerString: string
headerString
=
const header: readonly (keyof Object)[]
header
.
ReadonlyArray<keyof Object>.join(separator?: string): string

Adds all the elements of an array separated by the specified separator string.

@paramseparator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

join
(",");
// handle null or undefined values here
const
const replacer: (_key: unknown, value: unknown) => string
replacer
= (
_key: unknown
_key
: unknown,
value: unknown
value
: unknown) => (!!
value: unknown
value
?
value: {}
value
.
Object.toString(): string

Returns a string representation of an object.

toString
() : "");
const
const rowItems: string[]
rowItems
=
items: Object[]
items
.
Array<Object>.map<string>(callbackfn: (value: Object, index: number, array: Object[]) => string, thisArg?: any): string[]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
((
row: Object
row
) => {
return
const header: readonly (keyof Object)[]
header
.
ReadonlyArray<keyof Object>.map<string>(callbackfn: (value: keyof Object, index: number, array: readonly (keyof Object)[]) => string, thisArg?: any): string[]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
((
fieldName: keyof Object
fieldName
) =>
var JSON: JSON

An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

JSON
.
JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

@paramvalue A JavaScript value, usually an object or array, to be converted.

@paramreplacer A function that transforms the results.

@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

@throws{TypeError} If a circular reference or a BigInt value is found.

stringify
(
row: Object
row
[
fieldName: keyof Object
fieldName
],
const replacer: (_key: unknown, value: unknown) => string
replacer
)).
Array<string>.join(separator?: string): string

Adds all the elements of an array into a string, separated by the specified separator string.

@paramseparator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.

join
(",");
});
// join header and body, and break into separate lines
const
const csv: string
csv
= [
const headerString: string
headerString
, ...
const rowItems: string[]
rowItems
].
Array<string>.join(separator?: string): string

Adds all the elements of an array into a string, separated by the specified separator string.

@paramseparator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.

join
("\r\n");
return
const csv: string
csv
;
}