title: Python and JSON class
body: <p>I am having trouble with JSON, I can not find suitable information.</p>
  
  <p>Firstly, I managed to make the code to create an XML in python, but not JSON.</p>
  
  <p>I wonder how to transform the xml writer class below in order to convert equivalent to JSON Writer Class.</p>
  
  <pre><code>from lxml import etree
  import re
  
  class XmlWriter:
      # Function create a xml
      def createNode(self,nodeName, parentNode = '', withAttribs = {}, withValue = ''):
  
          if nodeName != '':
              nodeName = re.sub(r'/', '_', nodeName)
              nodeName = re.sub(r'#','id',nodeName)
              if parentNode == '':
                  # Create a parent node
                  createdNode = etree.Element(nodeName)
              else:                  
                  # Create a child node
                  createdNode = etree.SubElement(parentNode, nodeName)
  
              # Put the Attributs with value    
              if  withAttribs !={}:
                  for key,value in withAttribs.items():
                      createdNode.set(key,value)
  
              # Put the text content of the xml node
              if withValue != '':
                  createdNode.text = withValue
              else:
                  pass
  
              return createdNode
  
      # Print the XML for information
      def printXML(self,nodeName):
          print (etree.tostring(nodeName, pretty_print=True))
  
      # Save the XML on the file
      def saveXML(self,nodeName,filename):
          if nodeName != '' or filename !='':
              with open(filename, "w") as f:
                  f.write(etree.tostring(nodeName, pretty_print=True))
          else:
              return False
  </code></pre>
  
  <p>I try in several research attempting to create an equivalent of a "tree node" for example:</p>
  
  <pre><code>{"menu": {
    "id": "file",
    "popup": {
      "menuitem": [
        {"value": "New", "onclick": "CreateNewDoc()"},
        {"value": "Open", "onclick": "OpenDoc()"},
        {"value": "Close", "onclick": "CloseDoc()"}
      ]
    }
  }}
  </code></pre>
  
  <p>But how to create a class to get a result if you do bellow example:</p>
  
  <pre><code>n1 = CreateNode(name,parent_dependancy,value)...
  </code></pre>
  
  <p>Thank you very much</p>