Workflows and JSONs

I have a workflow step that creates this JSON structure when running a loop on some records:

{
	"data": [
		[
			{
				"invoice_id": "14592",
				"line_id": "1",
				"line_number": "1",
				"line_description": "blah",
				"line_amount": "0",
				"line_type": "DescriptionOnly"
			},
			{
				"invoice_id": "14592",
				"line_id": "2",
				"line_number": "2",
				"line_description": "blah",
				"line_amount": "1012.00",
				"line_type": "SalesItemLineDetail"
			}
		],
		[
			{
				"invoice_id": "14591",
				"line_id": "1",
				"line_number": "1",
				"line_description": "blah 1",
				"line_amount": "0",
				"line_type": "DescriptionOnly"
			},
			{
				"invoice_id": "14591",
				"line_id": "2",
				"line_number": "2",
				"line_description": "blah 1",
				"line_amount": "1012.00",
				"line_type": "SalesItemLineDetail"
			}
		]
	]
}

I haven't figured out how to convert it to the following JSON to be used as a source to upsert into a table:

{
	"data": [
		{
			"invoice_id": "14592",
			"line_id": "1",
			"line_number": "1",
			"line_description": "blah",
			"line_amount": "0",
			"line_type": "DescriptionOnly"
		},
		{
			"invoice_id": "14592",
			"line_id": "2",
			"line_number": "2",
			"line_description": "blah",
			"line_amount": "1012.00",
			"line_type": "SalesItemLineDetail"
		},
		{
			"invoice_id": "14591",
			"line_id": "1",
			"line_number": "1",
			"line_description": "blah 1",
			"line_amount": "0",
			"line_type": "DescriptionOnly"
		},
		{
			"invoice_id": "14591",
			"line_id": "2",
			"line_number": "2",
			"line_description": "blah 1",
			"line_amount": "1012.00",
			"line_type": "SalesItemLineDetail"
		}
	]
}

The Code that is generating such JSON is the following:

return parse_xml.data.Invoice.map( 
  single => {
    var jsonArray = [];
    var jsonObject; 

    var i = 0;
    var line;
    var line_type;   
    
    const invoice_id = single.Id[0];
    const lines = single.Line;
    const lines_count = lines.length;

    for (i == 0 ; i < lines_count ; i++) {
      line = lines[i];
      line_type = line.DetailType[0];

      if (line_type == "DescriptionOnly" || line_type == "SalesItemLineDetail" ) { 
        jsonObject = {'invoice_id': invoice_id, 
                      'line_id': line.Id[0], 
                      'line_number': line.LineNum[0],
                      'line_description': line.Description[0], 
                      'line_amount': (line.Amount?.length > 0 ? line.Amount[0] : "0"),
                      'line_type': line_type};
        jsonArray.push(jsonObject);
      }
    }
    return jsonArray;
  }
);

I know it is something simple, but I haven't been successful.
Thanks

HI there @Dataland,

Can you try the below?

return parse_xml.data.Invoice.map(single => {
  const jsonArray = [];
  const invoice_id = single.Id[0];
  const lines = single.Line ?? [];

  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    const line_type = line.DetailType?.[0];

    if (line_type === "DescriptionOnly" || line_type === "SalesItemLineDetail") {
      jsonArray.push({
        invoice_id,
        line_id: line.Id?.[0],
        line_number: line.LineNum?.[0],
        line_description: line.Description?.[0],
        line_amount: line.Amount?.[0] ?? "0",
        line_type
      });
    }
  }

  return jsonArray;
}).flat(); // ← This flattens [[...], [...]] to [...]

Thank you @MiguelOrtiz !!!
So simple... why didn't I think of it?
Anyhow, appreciate your solution. works perfectly.

1 Like