Search code examples
pythonpyparsing

How to parse this string of data structure with pyparsing


I have the following sample data that are stored in a file:

[
  { "keys": ["val1", "val2"], "args": { "arg1": "val1", "arg2": "val2" } },
  { "keys": ["val1", "val2", "val3"], "args": { "arg": "val" } },
  { "keys": ["val"], "args": {} }
]

As you may realise, this is a list of dictionaries. Each dictionary has key keys contains arbitrary length of list and key args contains a dictionary

How could I parse this sample data back into Python object

with open('file_name') as file:
    source = file.read()

data = how_to_parse(source)

for arr in data:
    print(arr)

# Expected result
# { "keys": ["val1", "val2"], "args": { "arg1": "val1", "arg2": "val2" } }
# { "keys": ["val1", "val2", "val3"], "args": { "arg": "val" } }
# { "keys": ["val"], "args": {} }

Solution

  • The pyparsing wiki includes this example https://pyparsing.wikispaces.com/file/view/parsePythonValue.py/31712649/parsePythonValue.py which I implemented at a time when ast.literal_eval was not yet available. Using this code, your expression can be parsed using:

    print listItem.parseString("""[
      { "keys": ["val1", "val2"], "args": { "arg1": "val1", "arg2": "val2" } },
      { "keys": ["val1", "val2", "val3"], "args": { "arg": "val" } },
      { "keys": ["val"], "args": {} }
    ]""")[0]
    

    which gives:

    [{'keys': ['val1', 'val2'], 'args': {'arg1': 'val1', 'arg2': 'val2'}}, 
     {'keys': ['val1', 'val2', 'val3'], 'args': {'arg': 'val'}}, 
     {'keys': ['val'], 'args': {}}]
    

    There are many more examples at https://pyparsing.wikispaces.com/Examples for your self-edification.