mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 07:46:16 -04:00
Added support for parsing JSON with number type values. Added support for non-array JSON objects. Added extra tests for JSON to CSV operation.
This commit is contained in:
parent
01f0625d6a
commit
8fa8e34027
3 changed files with 100 additions and 0 deletions
|
@ -51,6 +51,10 @@ class JSONToCSV extends Operation {
|
||||||
this.rowDelim = rowDelim;
|
this.rowDelim = rowDelim;
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
|
if (!(input instanceof Array)) {
|
||||||
|
input = [input];
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// If the JSON is an array of arrays, this is easy
|
// If the JSON is an array of arrays, this is easy
|
||||||
if (input[0] instanceof Array) {
|
if (input[0] instanceof Array) {
|
||||||
|
@ -89,6 +93,8 @@ class JSONToCSV extends Operation {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
escapeCellContents(data) {
|
escapeCellContents(data) {
|
||||||
|
if (typeof data === "number") data = data.toString();
|
||||||
|
|
||||||
// Double quotes should be doubled up
|
// Double quotes should be doubled up
|
||||||
data = data.replace(/"/g, '""');
|
data = data.replace(/"/g, '""');
|
||||||
|
|
||||||
|
|
|
@ -52,6 +52,7 @@ import "./tests/Image";
|
||||||
import "./tests/Jump";
|
import "./tests/Jump";
|
||||||
import "./tests/JSONBeautify";
|
import "./tests/JSONBeautify";
|
||||||
import "./tests/JSONMinify";
|
import "./tests/JSONMinify";
|
||||||
|
import "./tests/JSONtoCSV";
|
||||||
import "./tests/JWTDecode";
|
import "./tests/JWTDecode";
|
||||||
import "./tests/JWTSign";
|
import "./tests/JWTSign";
|
||||||
import "./tests/JWTVerify";
|
import "./tests/JWTVerify";
|
||||||
|
|
93
tests/operations/tests/JSONtoCSV.mjs
Normal file
93
tests/operations/tests/JSONtoCSV.mjs
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
/**
|
||||||
|
* JSON to CSV tests.
|
||||||
|
*
|
||||||
|
* @author mshwed [m@ttshwed.com]
|
||||||
|
*
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../TestRegister";
|
||||||
|
|
||||||
|
const EXPECTED_CSV_SINGLE = "a,b,c\r\n1,2,3\r\n";
|
||||||
|
const EXPECTED_CSV_MULTIPLE = "a,b,c\r\n1,2,3\r\n1,2,3\r\n";
|
||||||
|
const EXPECTED_CSV_EMPTY = "\r\n\r\n";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: strings as values",
|
||||||
|
input: JSON.stringify({a: "1", b: "2", c: "3"}),
|
||||||
|
expectedOutput: EXPECTED_CSV_SINGLE,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: numbers as values",
|
||||||
|
input: JSON.stringify({a: 1, b: 2, c: 3}),
|
||||||
|
expectedOutput: EXPECTED_CSV_SINGLE,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: numbers and strings as values",
|
||||||
|
input: JSON.stringify({a: 1, b: "2", c: 3}),
|
||||||
|
expectedOutput: EXPECTED_CSV_SINGLE,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: JSON as an array",
|
||||||
|
input: JSON.stringify([{a: 1, b: "2", c: 3}]),
|
||||||
|
expectedOutput: EXPECTED_CSV_SINGLE,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: multiple JSON values in an array",
|
||||||
|
input: JSON.stringify([{a: 1, b: "2", c: 3}, {a: 1, b: "2", c: 3}]),
|
||||||
|
expectedOutput: EXPECTED_CSV_MULTIPLE,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: empty JSON",
|
||||||
|
input: JSON.stringify({}),
|
||||||
|
expectedOutput: EXPECTED_CSV_EMPTY,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "JSON to CSV: empty JSON in array",
|
||||||
|
input: JSON.stringify([{}]),
|
||||||
|
expectedOutput: EXPECTED_CSV_EMPTY,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "JSON to CSV",
|
||||||
|
args: [",", "\\r\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue