Repository: bridgecrewio/checkov

Issue:
Add --compact behaviour to json output
**Describe the issue**

For the json output, it would be nice to have the possibility of using the '--compact' option as for the CLI.

As for the CLI, maybe we only want to know the "file_path" and the "file_line_range" without having the code itself.
The "code_block" in the json is really hard to read because it creates a field per line with the line number.
Checkov will copy that block for each check and makes the json really long if the block is long (e.g. kubernetes cluster description) and it has several vulnerabilities in it.
So it would be nice to be able to not include it in the json output.
Thanks in advance.

code_block example (for a GCP subnet) :
```
"code_block": [
	[
		24,
		"resource \"google_compute_subnetwork\" \"dummy\" {\n"
	],
	[
		25,
		"  name          = \"${var.name_root}-${random_pet.subnet.id}\"\n"
	],
	[
		26,
		"  region        = var.region\n"
	],
	[
		27,
		"  network       = google_compute_network.dummy.name\n"
	],
	[
		28,
		"  ip_cidr_range = \"10.40.1.0/24\"\n"
	],
	[
		29,
		"}\n"
	]
]

```
**Additional context**
Add any other context about the problem here.



Discussion:

Resolving patch:
diff --git a/checkov/common/runners/runner_registry.py b/checkov/common/runners/runner_registry.py
--- a/checkov/common/runners/runner_registry.py
+++ b/checkov/common/runners/runner_registry.py
@@ -165,6 +165,8 @@ def print_reports(
             if output_formats:
                 print(OUTPUT_DELIMITER)
         if "json" in config.output:
+            if config.compact and report_jsons:
+                self.strip_code_blocks_from_json(report_jsons)
             if not report_jsons:
                 print(dumps(Report(None).get_summary(), indent=4, cls=CustomJSONEncoder))
                 data_outputs['json'] = json.dumps(Report(None).get_summary(), cls=CustomJSONEncoder)
@@ -283,3 +285,12 @@ def get_enriched_resources(repo_roots: List[Union[str, os.PathLike]]) -> Dict[st
                                 "skipped_checks": skipped_checks,
                             }
         return enriched_resources
+
+    @staticmethod
+    def strip_code_blocks_from_json(report_jsons: List[Dict[str, Any]]) -> None:
+        for report in report_jsons:
+            results = report.get('results', {})
+            for key, result in results.items():
+                for result_dict in result:
+                    result_dict.pop('code_block', None)
+                    result_dict.pop('connected_node', None)


Test patch:
diff --git a/tests/common/runner_registry/example_s3_tf/main.tf b/tests/common/runner_registry/example_s3_tf/main.tf
new file mode 100644
--- /dev/null
+++ b/tests/common/runner_registry/example_s3_tf/main.tf
@@ -0,0 +1,3 @@
+resource "aws_s3_bucket" "test" {
+  bucket = "test"
+}
diff --git a/tests/common/runner_registry/test_runner_registry.py b/tests/common/runner_registry/test_runner_registry.py
--- a/tests/common/runner_registry/test_runner_registry.py
+++ b/tests/common/runner_registry/test_runner_registry.py
@@ -1,7 +1,9 @@
+import argparse
 import unittest
 
 import os
-
+import io
+from unittest.mock import patch
 from checkov.cloudformation.runner import Runner as cfn_runner
 from checkov.common.runners.runner_registry import RunnerRegistry
 from checkov.common.util.banner import banner
@@ -73,6 +75,59 @@ def verify_empty_report(self, test_files_dir, files=None):
             self.assertEqual(report.passed_checks, [])
         return runner_registry
 
+    def test_compact_json_output(self):
+        test_files_dir = os.path.dirname(os.path.realpath(__file__)) + "/example_s3_tf"
+        runner_filter = RunnerFilter(framework=None, checks=None, skip_checks=None)
+        runner_registry = RunnerRegistry(
+            banner, runner_filter, tf_runner(), cfn_runner(), k8_runner()
+        )
+        reports = runner_registry.run(root_folder=test_files_dir)
+
+        config = argparse.Namespace(
+            file=['./example_s3_tf/main.tf'],
+            compact=True,
+            output=['json'],
+            quiet=False,
+            soft_fail=False,
+            soft_fail_on=None,
+            hard_fail_on=None,
+            output_file_path=None,
+        )
+
+        with patch('sys.stdout', new=io.StringIO()) as captured_output:
+            runner_registry.print_reports(scan_reports=reports, config=config)
+
+        output = captured_output.getvalue()
+
+        assert 'code_block' not in output
+        assert 'connected_node' not in output
+
+    def test_non_compact_json_output(self):
+        test_files_dir = os.path.dirname(os.path.realpath(__file__)) + "/example_s3_tf"
+        runner_filter = RunnerFilter(framework=None, checks=None, skip_checks=None)
+        runner_registry = RunnerRegistry(
+            banner, runner_filter, tf_runner(), cfn_runner(), k8_runner()
+        )
+        reports = runner_registry.run(root_folder=test_files_dir)
+
+        config = argparse.Namespace(
+            file=['./example_s3_tf/main.tf'],
+            compact=False,
+            output=['json'],
+            quiet=False,
+            soft_fail=False,
+            soft_fail_on=None,
+            hard_fail_on=None,
+            output_file_path=None,
+        )
+
+        with patch('sys.stdout', new=io.StringIO()) as captured_output:
+            runner_registry.print_reports(scan_reports=reports, config=config)
+
+        output = captured_output.getvalue()
+
+        assert 'code_block' in output
+
 
 if __name__ == "__main__":
     unittest.main()