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

In the current case of converting json to json, I hope to get the value of a field as a json object #224

Closed
tb-artomu opened this issue Dec 25, 2024 · 2 comments

Comments

@tb-artomu
Copy link

input:

{
    "datas":[
        {
            "shipmentHeadersAll":{
                "custom_data":{
                    "shipwell_custom_data":{
                        "71471beb-33ee-4b4e-b08c-84c1ef11643d":"MCA",
                        "f0a133e6-a3b6-4ff5-ab30-047b8b56cb66":"CA-UNS-ECM",
                        "63d30d2f-b58d-4555-a52f-e62dac0e90e6":"SN",
                        "0ab71385-1fd6-4a9b-8ed5-c8891dcc7e24":"FOB HCM",
                        "f501bd0e-894c-42e8-ae81-32695ddcdc56":"5e89cd254aa146ec8b71dd48456cdddf",
                        "5621d57d-0bb0-477c-a8fa-319c75be3bf0":"COSCO SHIPPING LINES SOUTH CHINA CO., LTD SHUNDE BRANCH",
                        "54bb1a24-57a0-4334-9586-1854d2a39b7f":"FOB HCM",
                        "329036b8-29c8-44b9-bf73-6fcaae16afe1":"2024-11-28 20:13:30"
                    }
                }
            },
            "shipmentLinesAllList":[
                {
                    "custom_data":{
                        "shipwell_custom_data":{
                            "64873a22-cf7d-4a55-a401-ec305513d184":"909",
                            "18c7ebfd-a839-4548-9626-91e198d304c9":"Vacuum Cleaner",
                            "bf875d71-8a1c-494d-a7f3-4094efab5efc":"CA-UNS-ECM",
                            "ace6afc8-5659-4a7a-94a2-7038dbde49ab":"CA-UNS-ECM",
                            "fd27e38c-a23f-436e-b96e-eba66ce81a7f":"2024-12-24 15:44:40",
                            "94749118-809d-416d-bf3d-e15a55eaa290":"2024-11-30",
                            "fa71e7d2-e8a8-4ac7-aa26-387d3aa2fc19":"Midea",
                            "42ec395c-19ca-4260-8954-a80e98205651":312702,
                            "2abd26b0-bfd4-4964-bd8d-d2025c64f5c7":"529230",
                            "abe0ab67-43c4-4fea-93d3-713e04a1812e":"PR240921000079",
                            "b6893be8-9886-48da-af94-050e2d8d109a":"MCA",
                            "9d300c89-de30-4dba-8991-79b995b641db":"632 PR",
                            "eb42f39e-2b89-4096-809a-1cd058f44798":"1502"
                        }
                    },
                    "tmsShipmentHeaderId":"559a4fbb-ddbd-40ae-b115-1c0416ad23dd",
                    "tmsShipmentLineId":"a45b4bd7-405d-4b30-bab6-dfd0cc8ad9cb"
                }
            ]
        }
    ]
}

schema config

{
    "parser_settings": {
        "version": "omni.2.1",
        "file_format_type": "json"
    },
    "transform_declarations": {
        "FINAL_OUTPUT": { "object": {
            "items": { "array": [{ "xpath": "datas/*", "object": {
                "shipwell_custom_data":  {"xpath": "/shipmentHeadersAll/custom_data/shipwell_custom_data", "type": "string"}
        }}]
    }}}}
}

Currently incorrect output

[
	{
		"items": [
			{
				"shipwell_custom_data": "MCACA-UNS-ECMSNFOB HCM5e89cd254aa146ec8b71dd48456cdddfCOSCO SHIPPING LINES SOUTH CHINA CO., LTD SHUNDE BRANCHFOB HCM2024-11-28 20:13:30"
			}
		]
	}
]

### The expected result is a json object or json string

``` json
[
	{
		"items": [
			{
				"shipwell_custom_data": "{
                    "64873a22-cf7d-4a55-a401-ec305513d184":"909",
                    "18c7ebfd-a839-4548-9626-91e198d304c9":"Vacuum Cleaner",
                    "bf875d71-8a1c-494d-a7f3-4094efab5efc":"CA-UNS-ECM",
                    "ace6afc8-5659-4a7a-94a2-7038dbde49ab":"CA-UNS-ECM",
                    "fd27e38c-a23f-436e-b96e-eba66ce81a7f":"2024-12-24 15:44:40",
                    "94749118-809d-416d-bf3d-e15a55eaa290":"2024-11-30",
                    "fa71e7d2-e8a8-4ac7-aa26-387d3aa2fc19":"Midea",
                    "42ec395c-19ca-4260-8954-a80e98205651":312702,
                    "2abd26b0-bfd4-4964-bd8d-d2025c64f5c7":"529230",
                    "abe0ab67-43c4-4fea-93d3-713e04a1812e":"PR240921000079",
                    "b6893be8-9886-48da-af94-050e2d8d109a":"MCA",
                    "9d300c89-de30-4dba-8991-79b995b641db":"632 PR",
                    "eb42f39e-2b89-4096-809a-1cd058f44798":"1502"
                }"
			}
		]
	}
]
@jf-tech
Copy link
Owner

jf-tech commented Jan 2, 2025

@tb-artomu sorry we weren't in the office for about a couple of weeks.

The trick to solve your problem is to use the magic custom func copy:

(I added one more element under shipmentLinesAllList to also show case the arraying ability)

input:

{
    "datas":[
        {
            "shipmentHeadersAll":{
                "custom_data":{
                    "shipwell_custom_data":{
                        "71471beb-33ee-4b4e-b08c-84c1ef11643d":"MCA",
                        "f0a133e6-a3b6-4ff5-ab30-047b8b56cb66":"CA-UNS-ECM",
                        "63d30d2f-b58d-4555-a52f-e62dac0e90e6":"SN",
                        "0ab71385-1fd6-4a9b-8ed5-c8891dcc7e24":"FOB HCM",
                        "f501bd0e-894c-42e8-ae81-32695ddcdc56":"5e89cd254aa146ec8b71dd48456cdddf",
                        "5621d57d-0bb0-477c-a8fa-319c75be3bf0":"COSCO SHIPPING LINES SOUTH CHINA CO., LTD SHUNDE BRANCH",
                        "54bb1a24-57a0-4334-9586-1854d2a39b7f":"FOB HCM",
                        "329036b8-29c8-44b9-bf73-6fcaae16afe1":"2024-11-28 20:13:30"
                    }
                }
            },
            "shipmentLinesAllList":[
                {
                    "custom_data":{
                        "shipwell_custom_data":{
                            "64873a22-cf7d-4a55-a401-ec305513d184":"909",
                            "18c7ebfd-a839-4548-9626-91e198d304c9":"Vacuum Cleaner",
                            "bf875d71-8a1c-494d-a7f3-4094efab5efc":"CA-UNS-ECM",
                            "ace6afc8-5659-4a7a-94a2-7038dbde49ab":"CA-UNS-ECM",
                            "fd27e38c-a23f-436e-b96e-eba66ce81a7f":"2024-12-24 15:44:40",
                            "94749118-809d-416d-bf3d-e15a55eaa290":"2024-11-30",
                            "fa71e7d2-e8a8-4ac7-aa26-387d3aa2fc19":"Midea",
                            "42ec395c-19ca-4260-8954-a80e98205651":312702,
                            "2abd26b0-bfd4-4964-bd8d-d2025c64f5c7":"529230",
                            "abe0ab67-43c4-4fea-93d3-713e04a1812e":"PR240921000079",
                            "b6893be8-9886-48da-af94-050e2d8d109a":"MCA",
                            "9d300c89-de30-4dba-8991-79b995b641db":"632 PR",
                            "eb42f39e-2b89-4096-809a-1cd058f44798":"1502"
                        }
                    },
                    "tmsShipmentHeaderId":"559a4fbb-ddbd-40ae-b115-1c0416ad23dd",
                    "tmsShipmentLineId":"a45b4bd7-405d-4b30-bab6-dfd0cc8ad9cb"
                },
                {
                    "custom_data":{
                        "shipwell_custom_data":{
                            "64873a22-cf7d-4a55-a401-ec305513d184":"809",
                            "18c7ebfd-a839-4548-9626-91e198d304c9":"Bacuum Cleaner",
                            "bf875d71-8a1c-494d-a7f3-4094efab5efc":"ZA-UNS-ECM",
                            "ace6afc8-5659-4a7a-94a2-7038dbde49ab":"ZA-UNS-ECM",
                            "fd27e38c-a23f-436e-b96e-eba66ce81a7f":"3024-12-24 15:44:40",
                            "94749118-809d-416d-bf3d-e15a55eaa290":"3024-11-30",
                            "fa71e7d2-e8a8-4ac7-aa26-387d3aa2fc19":"Nidea",
                            "42ec395c-19ca-4260-8954-a80e98205651":412702,
                            "2abd26b0-bfd4-4964-bd8d-d2025c64f5c7":"629230",
                            "abe0ab67-43c4-4fea-93d3-713e04a1812e":"XR240921000079",
                            "b6893be8-9886-48da-af94-050e2d8d109a":"DCA",
                            "9d300c89-de30-4dba-8991-79b995b641db":"732 PR",
                            "eb42f39e-2b89-4096-809a-1cd058f44798":"8502"
                        }
                    },
                    "tmsShipmentHeaderId":"559a4fbb-ddbd-40ae-b115-1c0416ad23dd",
                    "tmsShipmentLineId":"a45b4bd7-405d-4b30-bab6-dfd0cc8ad9cb"
                }
            ]
        }
    ]
}

schema:

{
    "parser_settings": {
        "version": "omni.2.1",
        "file_format_type": "json"
    },
    "transform_declarations": {
        "FINAL_OUTPUT": { "object": {
            "items": { "array": [{ "xpath": "datas/*/shipmentLinesAllList/*", "object": {
                "shipwell_custom_data":  {"xpath": "custom_data/shipwell_custom_data", "custom_func": { "name": "copy"} }
        }}]
    }}}}
}

output:

$ ./op transform -i input.txt -s schema.txt
[
	{
		"items": [
			{
				"shipwell_custom_data": {
					"18c7ebfd-a839-4548-9626-91e198d304c9": "Vacuum Cleaner",
					"2abd26b0-bfd4-4964-bd8d-d2025c64f5c7": "529230",
					"42ec395c-19ca-4260-8954-a80e98205651": 312702,
					"64873a22-cf7d-4a55-a401-ec305513d184": "909",
					"94749118-809d-416d-bf3d-e15a55eaa290": "2024-11-30",
					"9d300c89-de30-4dba-8991-79b995b641db": "632 PR",
					"abe0ab67-43c4-4fea-93d3-713e04a1812e": "PR240921000079",
					"ace6afc8-5659-4a7a-94a2-7038dbde49ab": "CA-UNS-ECM",
					"b6893be8-9886-48da-af94-050e2d8d109a": "MCA",
					"bf875d71-8a1c-494d-a7f3-4094efab5efc": "CA-UNS-ECM",
					"eb42f39e-2b89-4096-809a-1cd058f44798": "1502",
					"fa71e7d2-e8a8-4ac7-aa26-387d3aa2fc19": "Midea",
					"fd27e38c-a23f-436e-b96e-eba66ce81a7f": "2024-12-24 15:44:40"
				}
			},
			{
				"shipwell_custom_data": {
					"18c7ebfd-a839-4548-9626-91e198d304c9": "Bacuum Cleaner",
					"2abd26b0-bfd4-4964-bd8d-d2025c64f5c7": "629230",
					"42ec395c-19ca-4260-8954-a80e98205651": 412702,
					"64873a22-cf7d-4a55-a401-ec305513d184": "809",
					"94749118-809d-416d-bf3d-e15a55eaa290": "3024-11-30",
					"9d300c89-de30-4dba-8991-79b995b641db": "732 PR",
					"abe0ab67-43c4-4fea-93d3-713e04a1812e": "XR240921000079",
					"ace6afc8-5659-4a7a-94a2-7038dbde49ab": "ZA-UNS-ECM",
					"b6893be8-9886-48da-af94-050e2d8d109a": "DCA",
					"bf875d71-8a1c-494d-a7f3-4094efab5efc": "ZA-UNS-ECM",
					"eb42f39e-2b89-4096-809a-1cd058f44798": "8502",
					"fa71e7d2-e8a8-4ac7-aa26-387d3aa2fc19": "Nidea",
					"fd27e38c-a23f-436e-b96e-eba66ce81a7f": "3024-12-24 15:44:40"
				}
			}
		]
	}
]

@jf-tech
Copy link
Owner

jf-tech commented Jan 5, 2025

@tb-artomu closing the ticket. if you have any further questions, feel free to reopen it.

@jf-tech jf-tech closed this as completed Jan 5, 2025
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