Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom_func uses properties from two arrays of objects #201

Closed
tb-artomu opened this issue May 10, 2023 · 4 comments
Closed

custom_func uses properties from two arrays of objects #201

tb-artomu opened this issue May 10, 2023 · 4 comments

Comments

@tb-artomu
Copy link

  1. input data
    {
    "count":7,
    "next":"xxx",
    "previous":"xxx",
    "results":[
    {
    "order_reference":"T3008659677",
    "order_date":"2023-01-06T12:37:37",
    "test_flag":true,
    "supplier":"https://XXXX",
    "currency_code":"GBP",
    "subtotal":"1.40",
    "tax":"8.40",
    "total":"9.80",
    "items":[
    {
    "url":"https://items",
    "part_number":"RESKU1",
    "supplier_sku_reference":"SUSKU1",
    "line_reference":"1",
    "quantity":3
    },
    {
    "url":"https://items2",
    "part_number":"RESKU2",
    "supplier_sku_reference":"SUSKU2",
    "line_reference":"2",
    "quantity":1
    }
    ],
    "goods":[
    {
    "g_url":"https://goods",
    "g_part_number":"RESKU1",
    "g_supplier_sku_reference":"SUSKU1",
    "g_line_reference":"1",
    "g_quantity":3
    },
    {
    "g_url":"https://goods2",
    "g_part_number":"RESKU2",
    "g_supplier_sku_reference":"SUSKU2",
    "g_line_reference":"2",
    "g_quantity":1
    }
    ]
    }
    ]}

  2. expected output:
    eg: use concat func = > iterm_url_number = items.url + “_” + g_part_number

  3. How to write schema?Is there a sample?
    {
    "parser_settings": {
    "version": "omni.2.1",
    "file_format_type": "json"
    },
    "transform_declarations": {
    "FINAL_OUTPUT": {
    "object": {
    "datas": {
    "array": [{
    "xpath": "/results/",
    "object": {
    "iterms_list": { "array": [ { "xpath": "items/
    ", "object": {
    "iterm_url": {
    "xpath": "url"
    },
    "iterm_url_number": {
    "custom_func": {
    "name": "concat",
    "args": [
    {"xpath": "url"},
    {"const": "_"},
    {"xpath": "../goods//g_part_number"}
    ]
    }
    }
    }} ] },
    "goods_list": { "array": [ { "xpath": "goods/
    ", "object": {
    "iterm_part_number": {
    "xpath": "g_part_number"
    }
    }} ] }
    }
    }]
    }
    }
    }
    }
    }

@jf-tech
Copy link
Owner

jf-tech commented May 11, 2023

  1. next time, please use ``` the triple ticks code block so your json text/schema would be more readable. See markdown on the triple tick usage (https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks).

  2. I'm following up with underlying xpath library omniparser utilizes, trying to see if there is a way to achieve what you want. See How to "zip" two lists together? antchfx/xpath#90

@jf-tech
Copy link
Owner

jf-tech commented May 11, 2023

Actually, i figured out a different way by using javascript_with_context (please read the linked doc for more context and understanding).

Input JSON:

{
    "count":7,
    "next":"xxx",
    "previous":"xxx",
    "results":[
        {
            "order_reference":"T3008659677",
            "order_date":"2023-01-06T12:37:37",
            "test_flag":true,
            "supplier":"https://XXXX",
            "currency_code":"GBP",
            "subtotal":"1.40",
            "tax":"8.40",
            "total":"9.80",
            "items":[
                {
                    "url":"https://items",
                    "part_number":"RESKU1",
                    "supplier_sku_reference":"SUSKU1",
                    "line_reference":"1",
                    "quantity":3
                },
                {
                    "url":"https://items2",
                    "part_number":"RESKU2",
                    "supplier_sku_reference":"SUSKU2",
                    "line_reference":"2",
                    "quantity":1
                }
            ],
            "goods":[
                {
                    "g_url":"https://goods",
                    "g_part_number":"RESKU1",
                    "g_supplier_sku_reference":"SUSKU1",
                    "g_line_reference":"1",
                    "g_quantity":3
                },
                {
                    "g_url":"https://goods2",
                    "g_part_number":"RESKU2",
                    "g_supplier_sku_reference":"SUSKU2",
                    "g_line_reference":"2",
                    "g_quantity":1
                }
            ]
        }
    ]
}

Schema:

{
    "parser_settings":{
        "version":"omni.2.1",
        "file_format_type":"json"
    },
    "transform_declarations":{
        "FINAL_OUTPUT":{
            "object":{
                "datas":{
                    "array":[
                        {
                            "xpath":"results/*",
                            "object":{
                                "iterms_list2":{
                                    "custom_func":{
                                        "name":"javascript_with_context",
                                        "args":[
                                            {
                                                "const":"var n = JSON.parse(_node); var ret = []; for (var i = 0; i < n['items'].length; i++) { var obj = new Object(); obj.iterm_url = n['items'][i]['url']; obj.iterm_url_number = obj.iterm_url + '_' + n['goods'][i]['g_part_number']; ret.push(obj); }; ret;"
                                            }
                                        ]
                                    }
                                },
                                "goods_list":{
                                    "array":[
                                        {
                                            "xpath":"goods/*",
                                            "object":{
                                                "iterm_part_number":{
                                                    "xpath":"g_part_number"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

Output by cli.sh

[
	{
		"datas": [
			{
				"goods_list": [
					{
						"iterm_part_number": "RESKU1"
					},
					{
						"iterm_part_number": "RESKU2"
					}
				],
				"iterms_list2": [
					{
						"iterm_url": "https://items",
						"iterm_url_number": "https://items_RESKU1"
					},
					{
						"iterm_url": "https://items2",
						"iterm_url_number": "https://items2_RESKU2"
					}
				]
			}
		]
	}
]

The key is the use of javascript_with_context which is the ultimate custom_func that provides you with the IDR context and enables you to do any complex logic.

The javascript code (flattened in the schema) is:

const ":"
var n = JSON.parse(_node);
var ret = [];
for (var i = 0; i < n['items'].length; i++) {
    var obj = new Object();
    obj.iterm_url = n['items'][i]['url'];
    obj.iterm_url_number = obj.iterm_url + '_' + n['goods'][i]['g_part_number'];
    ret.push(obj);
};
ret;

The code is fairly self-explanatory. Let me know if you have any further questions.

@jf-tech jf-tech closed this as completed May 11, 2023
@jf-tech
Copy link
Owner

jf-tech commented May 11, 2023

@tb-artomu shameless plug here: please consider being a sponsor to the project, one time or recurring, big or small, equally appreciated! Thanks!

@tb-artomu
Copy link
Author

OK, Thank you for your answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants