cancel
Showing results for 
Search instead for 
Did you mean: 

SAP PO JSON Respose - Parse to XML

0 Kudos
759

Hello All,

I have requirement to convert JSON response into XML and parse only the rows values into target system. can you help me to achiever it through XSLT or anyother mapping in SAP PO. Like For Columns "Workcenter_Code" value of first record is "S_A114M1" and value of second record is "S_A114M1" so I need only the all the 10 values for each record and map it to target structure and don't need other elements like outputs,rowlimitexceeded and transaction. I tried with message mapping but during conversion rows values are not getting converted as expected. Kindly help me on it.

{"outputs":

{"Result_Code":null,

"Result_Error":null,

"Result_Message":null},

"tables":[

{"columns":

["Workcenter_Code","Part_No","Job_No","Operation_Code","Log_Date","Workcenter_Status","Duration","Op_No","Update_Date","Shift"],

"rows":[

["S_A114M1","6026581","7603942","Laserschweißen (laserweld)","2023-07-01T05:00:00Z","09 Aus (Off)",8.00000,10,"2023-08-30T09:57:00Z","3rd"],

["S_A114M1","6026581","7603942","Laserschweißen (laserweld)","2023-07-03T13:00:00Z","09 Aus (Off)",8.00000,10,"2023-08-30T09:57:00Z","2nd"],],

"rowLimitExceeded":false}],

"transactionNo":"258654"

}

Output XML

<MessageType>

<Workcenter_Code>S_A114M1</Workcenter_Code>

<Part_No>6026581</Part_No>

View Entire Topic
VinodKumar19
Explorer
0 Kudos

This can be done using the JAVA Mapping. You can feed the JSON payload to the JAVA Mapping. i am writing the equivalent code for the java mapping below. You can add the number of fields as per the requirement.

Java Mapping Code snippet

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;

public class readJSON {

	public static void main(String[] args) {

		String input = "input JSON Here";
		String outputprefix= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
				+ "<ns0:Sender_MT xmlns:ns0=\"urn:abc.com:xyz\">";
		String outputsuffix= " \n</ns0:Sender_MT>";
		String outputcontent= "";
		String output= "";
		ArrayList<String> tempvalues = new ArrayList<String>();

		JSONObject jsonObject= new JSONObject(input);

		JSONArray array= jsonObject.getJSONArray("tables")
				.getJSONObject(0)
				.getJSONArray("rows");


		for (int i =0; i<=array.length()-1;i++) {

			String wrkcntr= array.getJSONArray(i).getString(0);
			String partno = array.getJSONArray(i).getString(1);
			String jobno = 	array.getJSONArray(i).getString(2);
			String opcode = array.getJSONArray(i).getString(3);

			String temp = "	<Message_Data>\r\n"
					+ "      <Workcenter_Code>"+wrkcntr+"</Workcenter_Code>\r\n"
					+ "      <Part_No>"+partno+"</Part_No>\r\n"
					+ "      <Job_No>"+jobno+"</Job_No>\r\n"
					+ "      <Operation_Code>"+opcode+"</Operation_Code>\r\n"
					+ "   </Message_Data>";

			tempvalues.add(temp);

		}

		outputcontent= tempvalues.toString().replace("[", "").replace("]", "").replace(",", "\n");

		output = outputprefix + outputcontent + outputsuffix;

		System.out.println(output);

	}

}<br>

Just pass the above generated result to the output stream. The output would look like below XML.

Output

<strong><?xml version="1.0" encoding="UTF-8"?>
<ns0:Sender_MT xmlns:ns0="urn:abc.com:xyz">
	<Message_Data>
      <Workcenter_Code>S_A114M1</Workcenter_Code>
      <Part_No>6026581</Part_No>
      <Job_No>7603942</Job_No>
      <Operation_Code>Laserschwei�en (laserweld)</Operation_Code>
   </Message_Data>
 	<Message_Data>
      <Workcenter_Code>S_A114M1</Workcenter_Code>
      <Part_No>6026581</Part_No>
      <Job_No>7603942</Job_No>
      <Operation_Code>Laserschwei�en (laserweld)</Operation_Code>
   </Message_Data> 
</ns0:Sender_MT>
</strong>

You need to install the JSON library to the Archive.

You can read more about this in the below blog.

Read/Parse JSON Data in Java Mapping: A step by step guide

Thanks Vinod.... I will try it out with above Java Program