One Star

converting XML to JSON

Hi All,
I need to covert a XML to json, I am using talend 5.0,2 version, please suggest me how can I achieve the below sample working.
Sample Input XML:
<student>
<name>john</name>
<class>8</class>
<age>12</age>
<subject>
<subject_name>Physics</subject_name>
<score>80</score>
<grade>B</grade>
<percentage>75%</percentage>
</subject>
<subject>
<subject_name>Chemistry</subject_name>
<score>85</score>
<grade>B</grade>
<percentage>82%</percentage>
</subject>
<subject>
<subject_name>Computers</subject_name>
<score>90</score>
<grade>A</grade>
<percentage>87%</percentage>
</subject>
<student>
Expected Output:
{
"student": {
"name": "john",
"class": "8",
"age": "12",
"subject":
}
}
3 REPLIES
Moderator

Re: converting XML to JSON

Hi,
You can use a tFileInputXML to read an XML structured file and extracts data, then link it to a tWriteJsonField to construct a json string, then, output the json string to a file with tFileOutputDelimited, the job design looks like:
tFileInputXML--main--tWriteJsonField--main-->tFileOutputDelimited
For more details, please see my screenshots and let me know if you have any questions.

Best regar
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
One Star

Re: converting XML to JSON

Hi Sabrina,
Thanks for your reply, it helped me.
One more clarification, I am able to make only one loop element, what if I have multiple elements to be looped.
How do we achieve that? and
can you please brief me the importance of group_id in this job.
example:
<student>
<name>john</name>
<class>8</class>
<age>12</age>
<subject>
<subject_name>Physics</subject_name>
<score>80</score>
<grade>B</grade>
<percentage>75%</percentage>
</subject>
<subject>
<subject_name>Chemistry</subject_name>
<score>85</score>
<grade>B</grade>
<percentage>82%</percentage>
</subject>
<address_details>
<address>
<address_type>Temp Address</address_type>
<address_1>main</address_1>
<address_2>India</address_2>
<address_3>Delhi</address_3>
</address>
<address>
<address_type>Permanent Address</address_type>
<address_1>main</address_1>
<address_2>USA</address_2>
<address_3>Newyork</address_3>
</address>
</address_details>
</student>
Community Manager

Re: converting XML to JSON

Hi Sabrina,
Thanks for your reply, it helped me.
One more clarification, I am able to make only one loop element, what if I have multiple elements to be looped.
How do we achieve that? and
can you please brief me the importance of group_id in this job.

I have replied you in your another 31847, about group_id in this job, it is set with a fixed value and will be used as a group element on tWriteJsonField so as to generate only one string output, otherwise, it will generate multiple string outputs for each incoming data without group_id.
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business