jf-tech / omniparser

omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
MIT License
931 stars 68 forks source link

How to use nested custom_func, such as using upper+concat function #200

Closed tb-artomu closed 1 year ago

tb-artomu commented 1 year ago

input: { "tracking_number": "1z9999999999999999" } expected output { "tracking_number": "1Z9999999999999999_ORD" }

How to write schema json? { "parser_settings": { "version": "omni.2.1", "file_format_type": "json" }, "transform_declarations": { "FINAL_OUTPUT": { "object": { "tracking_number": { "custom_func": [ { "name": "upper", "args": [ { "xpath": "tracking_number" } ] }, { "name": "concat", "args": [ { "xpath": "tracking_number" } ,{ "const": "_ORD" }] } ] } } } } }

jf-tech commented 1 year ago

Sample input:

TRACKING_NUMBER,NAME
1z12345e1512345676,Joe

Sample Schema:

{
    "parser_settings": {
        "version": "omni.2.1",
        "file_format_type": "csv2"
    },
    "file_declaration": {
        "delimiter": ",",
        "records": [
            {
                "min": 1, "max": 1,
                "header": "^TRACKING_NUMBER,NAME$"
            },
            {
                "is_target": true,
                "columns": [
                    { "name": "TRACKING_NUMBER" },
                    { "name": "NAME" }
                ]
            }
        ]
    },
    "transform_declarations": {
        "FINAL_OUTPUT": { "object": {
            "name": { "xpath": "NAME" },
            "tracking_number": { "custom_func": {
                "name": "concat",
                "args": [
                    { "custom_func": {
                        "name": "upper",
                        "args": [{ "xpath": "TRACKING_NUMBER" }]
                    }},
                    { "const": "_" },
                    { "custom_func": {
                        "name": "upper",
                        "args": [{ "xpath": "NAME" }]
                    }}
                ]
            }}
        }}
    }
}

Sample output:

    {
        "name": "Joe",
        "tracking_number": "1Z12345E1512345676_JOE"
    }

For more custom_func usage, check https://github.com/jf-tech/omniparser/blob/master/doc/use_of_custom_funcs.md, "3. Chaining/Composability"