title: Pyparsing: Parsing semi-JSON nested plaintext data to a list
body: <p>I have a bunch of nested data in a format that loosely resembles JSON:</p>
  <pre><code>company=&quot;My Company&quot;
  phone=&quot;555-5555&quot;
  people=
  {
      person=
      {
          name=&quot;Bob&quot;
          location=&quot;Seattle&quot;
          settings=
          {
              size=1
              color=&quot;red&quot;
          }
      }
      person=
      {
          name=&quot;Joe&quot;
          location=&quot;Seattle&quot;
          settings=
          {
              size=2
              color=&quot;blue&quot;
          }
      }
  }
  places=
  {
      ...
  }
  </code></pre>
  <p>There are many different parameters with varying levels of depth--this is just a very small subset.</p>
  <p>It also might be worth noting that when a new sub-array is created that there is always an equals sign followed by a line break followed by the open bracket (as seen above).</p>
  <p>Is there any simple looping or recursion technique for converting this data to a system-friendly data format such as arrays or JSON? I want to avoid hard-coding the names of properties. I am looking for something that will work in Python, Java, or PHP. Pseudo-code is fine, too.</p>
  <p>I appreciate any help.</p>
  <p>EDIT: I discovered the Pyparsing library for Python and it looks like it could be a big help. I can't find any examples for how to use Pyparsing to parse nested structures of unknown depth. Can anyone shed light on Pyparsing in terms of the data I described above?</p>
  <p>EDIT 2: Okay, here is a working solution in Pyparsing:</p>
  <pre><code>def parse_file(fileName):
  
  #get the input text file
  file = open(fileName, &quot;r&quot;)
  inputText = file.read()
  
  #define the elements of our data pattern
  name = Word(alphas, alphanums+&quot;_&quot;)
  EQ,LBRACE,RBRACE = map(Suppress, &quot;={}&quot;)
  value = Forward() #this tells pyparsing that values can be recursive
  entry = Group(name + EQ + value) #this is the basic name-value pair
  
  
  #define data types that might be in the values
  real = Regex(r&quot;[+-]?\d+\.\d*&quot;).setParseAction(lambda x: float(x[0]))
  integer = Regex(r&quot;[+-]?\d+&quot;).setParseAction(lambda x: int(x[0]))
  quotedString.setParseAction(removeQuotes)
  
  #declare the overall structure of a nested data element
  struct = Dict(LBRACE + ZeroOrMore(entry) + RBRACE) #we will turn the output into a Dictionary
  
  #declare the types that might be contained in our data value - string, real, int, or the struct we declared
  value &lt;&lt; (quotedString | struct | real | integer)
  
  #parse our input text and return it as a Dictionary
  result = Dict(OneOrMore(entry)).parseString(inputText)
  return result.dump()
  </code></pre>
  <p>This works, but when I try to write the results to a file with json.dump(result), the contents of the file are wrapped in double quotes. Also, there are <code>\n</code> chraacters between many of the data pairs. I tried suppressing them in the code above with <code>LineEnd().suppress() </code>, but I must not be using it correctly.</p>