Convert any JSON to name/value pairs
Question: I have a source JSON that can be in any potential format. For storage &
processing purposes, I'd like to save this data in 2 column format.

For example, I'd like the following JSON:

    
    
    "record" {
      "name1": "value1",
      "name2": "value2",
      "parameters": {
        "param": {},
        "paramSet": {
          "items": [{
            "id": "id1"
          }, {
            "id": "id2"
          }]
        }
      }
    }
    

To be converted to the following CSV-like format:

    
    
    record:name1 , "value1"
    record:name2 , "value2"
    record:parameters:param , ""
    record:parameters:paramSet:items#0:id , "id1"
    record:parameters:paramSet:items#1:id , "id2"
    

My questions are:

  1. Is there a formal name for this transformation (so that I can search better).
  2. Is there a standard or convention for representing JSON in a 2-column format like this?
  3. Are there any libraries in Python that can do this for me?
  4. Are there libraries on other major programming languages that will make it easier to implement this? 

Thanks in advance.


Answer: First I made json correct:

    
    
    {
      "record": {
        "name1": "value1",
        "name2": "value2",
        "parameters": {
          "param": {},
          "paramSet": {
            "items": [
              {
                "id": "id1"
              },
              {
                "id": "id2"
              }
            ]
          }
        }
      }
    }
    

Next some code for recursive mocking json

    
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import json
    
    
    class Thing(object):
        data = ''
        output = []
    
        def __init__(self, file_name):
            with open(file_name) as data_file:
                self.data = json.load(data_file)
    
        def mock(self):
            for (i, item) in enumerate(self.data):
                if type(self.data[item]) == dict:
                    self._recursive(self.data[item], item)
            for (i, data) in enumerate(self.output):
                print(data)
    
        def _recursive(self, request_data, path):
            for (i, item) in enumerate(request_data):
                if type(request_data[item]) == dict:
                    if len(request_data[item]) > 0:
                        path2 = "{}:{}".format(path, item)
                        self._recursive(request_data[item], path2)
                    else:
                        self.output.append("{}:{}, \"\"".format(path, item))
                elif type(request_data[item]) == list:
                    for (j, list_item) in enumerate(request_data[item]):
                        path2 = "{}:{}#{}".format(path, item, j)
                        self._recursive(request_data[item][j], path2)
                else:
                    self.output.append("{}:{}, {}".format(path, item, request_data[item]))
    
    thing = Thing("input.json")
    thing.mock()
    

Following code will output:

    
    
    record:name1, value1
    record:name2, value2
    record:parameters:paramSet:items#0:id, id1
    record:parameters:paramSet:items#1:id, id2
    record:parameters:param, ""
    

