Merge "jsonpoll: flattening json data to one line per field."
diff --git a/jsonpoll/jsonpoll.py b/jsonpoll/jsonpoll.py
index f69fac3..bee52a0 100755
--- a/jsonpoll/jsonpoll.py
+++ b/jsonpoll/jsonpoll.py
@@ -23,7 +23,6 @@
 import socket
 import sys
 import tempfile
-import textwrap
 import time
 import urllib2
 import options
@@ -61,17 +60,39 @@
                                'api/radio': self.api_radio_output_file}
     self.last_response = None
 
+  def _FlatObject(self, base, obj, out):
+    """Open json object to a list of strings.
+
+    Args:
+      base: is a string that has a base name for a key.
+      obj: is a JSON object that will be flatten.
+      out: is an output where strings will be appended.
+
+    Example:
+         data = {"a": 1, "b": { "c": 2, "d": 3}}
+         out = []
+         _FlatObject('', data, out)
+
+         out will be equal to
+           [u'/a=1', u'/b/c=2', u'/b/d=3']
+    """
+    for k, v in obj.items():
+      name = base + '/' + k
+      if isinstance(v, dict):
+        self._FlatObject(name, v, out)
+      else:
+        val = '%s=' % name
+        out.append(val + str(v))
+
   def WriteToStderr(self, msg, is_json=False):
     """Write a message to stderr."""
     if is_json:
-      # Make the json easier to parse from the logs.
       json_data = json.loads(msg)
-      json_str = json.dumps(json_data, sort_keys=True, indent=2,
-                            separators=(',', ': '))
-      # Logging pretty-printed json is like logging one huge line. Logos is
-      # configured to limit lines to 768 characters. Split the logged output at
-      # half of that to make sure logos doesn't clip our output.
-      sys.stderr.write('\n'.join(textwrap.wrap(json_str, width=384)))
+      flat_data = []
+      self._FlatObject('', json_data, flat_data)
+      # Make the json easier to parse from the logs.
+      for s in flat_data:
+        sys.stderr.write('%s\n' % s)
       sys.stderr.flush()
     else:
       sys.stderr.write(msg)
diff --git a/jsonpoll/jsonpoll_test.py b/jsonpoll/jsonpoll_test.py
index d8431f0..f4f0240 100644
--- a/jsonpoll/jsonpoll_test.py
+++ b/jsonpoll/jsonpoll_test.py
@@ -120,6 +120,12 @@
       output = ''.join(line.rstrip() for line in f)
       self.assertEqual('', output)
 
+  def testFlatObject(self):
+    obj = {'key1': 1, 'key2': {'key3': 3, 'key4': 4}}
+    got = []
+    self.poller._FlatObject('base', obj, got)
+    want = ['base/key1=1', 'base/key2/key3=3', 'base/key2/key4=4']
+    self.assertEqual(got.sort(), want.sort())
 
 if __name__ == '__main__':
   unittest.main()