Skip to content

Flatten a Parent-child json #280

@vermapraveen

Description

@vermapraveen

hi @Courela

trying to find some nested loop kind of functionality to flatten below parent-child structure. Can you suggest how to proceed on this:

Input:

{
	"Path": "Type1",
	"Children": [
		{
			"Path": "Type1~Cat1",
			"Items": [ 
                                         { "Id": "66717101" },
				         { "Id": "66717102" }
			           ],
			"Children": [
				{
					"Path": "Type1~Cat1~Sku1",
					"Items": [ { "Id": "66717101" } ],
					"Children": [
						{
							"Path": "Type1~Cat1~Sku2~Model1",
							"Items": [ { "Id": "66717102" } ]
						},
						{
							"Path": "Type1~Cat1~Sku1~Model1",
							"Items": [ { "Id": "66717101" }, 
                                                                       { "Id": "66717102" } ]
						}
					]
				},
				{
					"Path": "Type1~Cat1~Sku2",
					"Items": [ { "Id": "66717101" } ],
					"Children": [
						{
							"Path": "Type1~Cat1~Sku2~Model1",
							"Items": [ { "Id": "66717722" } ]
						}
					]
				}
			]
		},
		{
			"Path": "Type1-Cat2",
			"Children": []
		},
		{
			"Path": "Type1-Cat3",
			"Children": []
		}
	],
	"Items": [ { "Id": "66717766" },
		       { "Id": "6828147" } ]
}

Output:

[
	{
		"Id": "66717101",
		"Path": [
			"Type1~Cat1",
			"Type1~Cat1~Sku1",
			"Type1~Cat1~Sku2~Model1",
			"Type1~Cat1~Sku2"
		]
	},
	{
		"Id": "66717102",
		"Path": [
			"Type1~Cat1",
			"Type1~Cat1~Sku2~Model1",
			"Type1~Cat1~Sku1~Model1"
		]
	},
	{
		"Id": "66717722",
		"Path": [
			"Type1~Cat1~Sku2~Model1"
		]
	},
	{
		"Id": "66717766",
		"Path": [
			"Type1"
		]
	},
	{
		"Id": "6828147",
		"Path": [
			"Type1"
		]
	}
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions