Convert an XML to JSON using ansible

Hi,

Is there a filter or any work around to convert an XML to JSON using ansible?

Thanks.

Hi,

There is an xml module (https://docs.ansible.com/ansible/2.4/xml_module.html) that would allow you to extract elements of the target and output them through a Json filter however without further logical manipulation you will just end up with a list of all elements.

You have several ways to create data structures (persistent variables) within the Ansible code itself which would solve the problem of the single long list of elements. Push the variables through a json filter and it solves the problem problem of actually using json to format data intelligent, which only leaves correctly creating/populating the data structures in the Ansible code as an outstanding issue.

I think that will work but without more information about specifically what you would like to pull from xml → output to json it’s basically just a guess.

Why would you possibly want to use ‘ansible’ to do that ?

There are a zillion ways to do it in various languages if you do a little google searching.

Try seaching for “convert xml to json python” for some examples…

Hi,

Thank you very much for the response. First let me explain what I'm trying to do here and then what I have done so far.
I'm trying to call and rest api which I'm using URI module to do that. Success response of this api returns a xml response not json. But my end user needs the output from my ansible playbook to be json so others can consume it for other processes. This xml response comes in the content of the response. E.g. return_content : yes . So this content is a string which is in xml format. I need to sanitize it to remove characters like \n (new line). That's been done using regex_replace. So then we get the plain xml. Now this xml output needs to be shown in json manner.
What i have been thinking and trying to do so far is create a python scrip which does the job. Then call the method in python script as a filter e.g. "{{ xml_response | xml_to_json}}". But stucked here on implementing this. Are there any easier work around for this?

The problem is that there is no single standard for converting XML to JSON.

The following site shows that there are at least 7 conversion methods to convert XML to JSON: http://wiki.open311.org/JSON_and_XML_Conversion/

Is it possible that the API you are calling would utilize a Accept: application/json header that would return the result in JSON instead of XML?

Otherwise, writing your own filter might be required.

Hi,

THe quickest way is to create your own filter:

  1. Create a ‘filter_plugins’ directory (in the same directory as you playbook or roles).
  2. Create a xml2json.py file in that directory with the following content:

############## cut below

class FilterModule(object):

def filters(self):
return {
‘xml2json’: self.xml2json,
}

def xml2json(self, value):
import xmltodict, json
return json.dumps(xmltodict.parse(value))

############# cut above

Once its there - you can call the filter:

set_fact:
content_json: “{{ content_xml | xml2json }}”

You can obviously also call this ‘in place’ as any other filter.

kind regards
Pshem

Thanks this solution works perfectly well. Just for the record this piece of code needs python package python-xmltodict installed. Thanks