JsonToTable

Accepts a json object and returns a markdown. Supports clickable links.

python · Filters And Transformers

Source

import json

import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401


def main():
    args = demisto.args()

    value = args.get("value")
    if from_str_value := get_value_from_str(value):
        value = from_str_value

    title = args.get("title")
    headers = argToList(args.get("headers"))
    url_keys = argToList(args.get("url_keys"))
    is_auto_json_transform = argToBoolean(args.get("is_auto_json_transform", False))
    json_transform_properties = args.get("json_transform_properties")
    json_transformers = {}
    if json_transform_properties:
        json_transform_properties = safe_load_json(json_transform_properties)
        for header_key, values in json_transform_properties.items():
            json_transformers[header_key] = JsonTransformer(**values)
    markdown = tableToMarkdown(
        title,
        value,
        headers=headers,
        json_transform_mapping=json_transformers,
        is_auto_json_transform=is_auto_json_transform,
        url_keys=url_keys,
    )

    return_results(
        CommandResults(
            readable_output=markdown,
            raw_response=markdown,
        )
    )


def get_value_from_str(value: Any):
    """
    Load Json from value in case of string value
    """
    str_value = None
    if isinstance(value, str):
        str_value = value

    # in case of str value when using this automation as transformer - the value will be in list as [str]
    if isinstance(value, list) and len(value) == 1 and isinstance(value[0], str):
        str_value = value[0]
    try:
        return json.loads(str_value) if str_value else None
    except json.JSONDecodeError:
        return str_value


if __name__ in ("__main__", "__builtin__", "builtins"):
    main()

README

Accepts a json object and returns a markdown.

Script Data


Name Description
Script Type python3
Tags transformer, entirelist, general
Cortex XSOAR Version 5.5.0

Inputs


Argument Name Description
value The json to transform to a markdown table.
title The markdown title.
headers A comma-separated list of table header values. Default will include all available table headers.
is_auto_json_transform Try to auto json transform.
json_transform_properties A json to transform the value to strings. The syntax is: `{“header_key”: {“keys”: [<item1>, …], “is_nested”: true/false}}`
url_keys Comma-separated list of keys in the given JSON table that should be turned into a clickable URL.

Outputs


There are no outputs for this script.

Script Examples

Example command

!JsonToTable value=`[{"name": "name1", "value": "val1"}, {"name": "name2", "value" : "val2"}]`

Context Example

{}

Human Readable Output

name value
name1 val1
name2 val2

Example command

!JsonToTable value=`[{"name": "name1", "value": "val1"}, {"name": "name2", "value" : "val2"}]` headers=name

Context Example

{}

Human Readable Output

name
name1
name2

Example command

!JsonToTable value=`[{"name": {"first": "a", "second": "b", "not_important": "no"}, "value": "val1"}, {"name": {"first": "c", "second": "d", "not_important": "no"}, "value": "val2"}]` is_auto_json_transform=true

Context Example

{}

Human Readable Output

name value

first: a
second: b
not_important: no
val1

first: c
second: d
not_important: no
val2

Example command

!JsonToTable value=`[{"name": {"first": "a", "second": "b", "not_important": "no"}, "value": "val1"}, {"name": {"first": "c", "second": "d", "not_important": "no"}, "value": "val2"}]` json_transform_properties=`{"name": {"keys": ["first", "second"]}}`

Context Example

{}

Human Readable Output

name value

first: a
second: b
val1

first: c
second: d
val2

Example command

!JsonToTable value=`[{"name": {"first": {"a": "val"}, "second": "b", "not_important": "no"}, "value": "val1"}, {"name": {"first": {"a": "val2"}, "second": "d", "not_important": "no"}, "value": "val2"}]` json_transform_properties=`{"name": {"keys": ["a", "second"], "is_nested": "true"}}`

Context Example

{}

Human Readable Output

name value
first:
a: val

second: b
val1
first:
a: val2

second: d
val2