Convert json to xml


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert json to xml
# 1  
Old 05-14-2015
Convert json to xml

Hello everyone,

I have created a workflow that will pull down data via a RESTful API in JSON, then my code parses and modifies said data. The problem I have is that these APIs I am working with only accept XML to PUT/POST data, and I am looking for an easy way to convert my JSON file to XML.

I did a search on the forums here and google searched a bit and did not find a solution to my problem yet. My preferred language is shell/bash, and while I did find some Python code that does this, my whole work flow is written in shell and uses jq.

Any ideas on how to convert a JSON file to XML in bash? I am hoping I don't have to build the XML on the fly and can just convert it.

I am working on OS X and Debian Wheezy and Ubuntu 14 if that matters. My computer is a Mac that has several VMs of Linux running.
# 2  
Old 05-15-2015
If you can install PHP you may try this its easy


PHP Code:
[akshay@localhost JSON2XML]$ cat test.php
<?php

/**
 * Convert JSON to XML
 * @param string    - json
 * @return string   - XML
 */
function json_to_xml($json)
{
    include_once(
"XML/Serializer.php");

    
$options = array (
      
'addDecl' => TRUE,
      
'encoding' => 'UTF-8',
      
'indent' => '  ',
      
'rootName' => 'json',
      
'mode' => 'simplexml'
    
);

    
$serializer = new XML_Serializer($options);
    
$obj json_decode($json);
    if (
$serializer->serialize($obj)) {
        return 
$serializer->getSerializedData();
    } else {
        return 
null;
    }
}



$sample = <<<EOF
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}
EOF;


    echo 
json_to_xml($sample);
?>
PHP Code:
[akshay@localhost JSON2XML]$ php test.php
<?xml version="1.0" encoding="UTF-8"?>
<json>
  <menu>
    <id>file</id>
    <value>File</value>
    <popup>
      <menuitem>
        <value>New</value>
        <onclick>CreateNewDoc()</onclick>
      </menuitem>
      <menuitem>
        <value>Open</value>
        <onclick>OpenDoc()</onclick>
      </menuitem>
      <menuitem>
        <value>Close</value>
        <onclick>CloseDoc()</onclick>
      </menuitem>
    </popup>
  </menu>
</json>

https://drive.google.com/file/d/0B6_...ew?usp=sharing
# 3  
Old 05-16-2015
There are a number of Python libraries than will convert JSON to XML
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert any shell command output to JSON format?

Hi All, I am new to shell scripting, Need your help in creating a shell script which converts any unix command output to JSON format output. example: sample df -h command ouput : Filesystem size used avail capacity Mounted /dev/dsk/c1t0d0s0 8.1G 4.0G 4.0G 50% /... (13 Replies)
Discussion started by: balu1234
13 Replies

2. UNIX for Beginners Questions & Answers

Convert String to an Array using shell scripting in JSON file.

This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array... (11 Replies)
Discussion started by: vinshas1
11 Replies

3. Shell Programming and Scripting

UNIX or Perl script to convert JSON to CSV

Is there a Unix or Perl script that converts JSON files to CSV or tab delimited format? We are running AIX 6.1. Thanks in advance! (1 Reply)
Discussion started by: warpmail
1 Replies

4. Shell Programming and Scripting

How to convert xml to csv ?

I am in need of converting billions of XML into csv file to load data to DB, i have found the below code in perl but not sure why it's not working properly. CODE: #!/usr/bin/perl # Script to illustrate how to parse a simple XML file # and pick out all the values for a specific element, in... (1 Reply)
Discussion started by: rspwilliam
1 Replies

5. Shell Programming and Scripting

Convert xml to csv

I need to convert below xml code to csv. I searched other posts as well but this post (_https://www.unix.com/shell-programming-scripting/174417-extract-parse-xml-data-statistic-value-csv.html) gives "sed command garbled" error. As of now I have written a long script to do it, but can it be done with... (7 Replies)
Discussion started by: dineshydv
7 Replies

6. Programming

[XQuery] How to Convert from JSON Message to XML Message with XQuery

Hi guys, I'm in a job of converting a restful webservice to soap. Tool for convertation uses XQuery. Now i need to convert a message like this: { "firstName": "John", "midName": null, "lastName": "Smith", "married": false, "address": { "streetAddress": "21 2nd... (5 Replies)
Discussion started by: tien86
5 Replies

7. Shell Programming and Scripting

awk convert xml to csv

Hi, I have an xml file and I want to convert it with awk in to a csv file Test.xml <Worksheet ss:Name="Map1"> <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="60"> <Row> <Cell><Data... (6 Replies)
Discussion started by: research3
6 Replies

8. Shell Programming and Scripting

Convert XML to CSV format

Can any one give the idea on this, please. I have the following XML file and wants to convert into CSV(comma separated value) format. <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE Waveset PUBLIC 'waveset.dtd' 'waveset.dtd'> <Waveset> <Object name='ra8736'> <Attribute name='ADDRESS'... (2 Replies)
Discussion started by: kumar04
2 Replies

9. Shell Programming and Scripting

how to convert a html file to an .xml one

Hii every one. I have lots of .html files like: <html> <title> </title> <head> </head> <body> <ul id="SummaryRatings">4 stars </ul> <ul id="summaryList"> <p><b> Aurhor</b> </p> <p>wsfwfrfrtgretryetyrtyty</p> </ul> ...... ....... </body> </html> (6 Replies)
Discussion started by: kheyali Mitra
6 Replies

10. Shell Programming and Scripting

Help to convert XML to CSV

Apologies if this has already been covered in this site somewhere, I did try looking but without any success. I am new to the whole XML thing, very late starter, and have a requirement to convert an XML fiule to a CSV fomat. I am crrently working on a Solaris OS. Does anyone have any suggestions,... (2 Replies)
Discussion started by: rossingi_33
2 Replies
Login or Register to Ask a Question