title: How to convert socket data to json object or to Dictionary with Python 3
body: <p>So I am trying to convert data from socket to json object.
  below is my python code that gets data from socket server.</p>
  
  <pre><code>import socket
  
  HOST = "myHOST"
  PORT = myPORT
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((myHOST, myPORT))
      s.sendall(b" ")
      buffer_size = 4096
      print("Received: ")
      while True:
          data = s.recv(buffer_size)
          print(repr(data))
  s.close()
  </code></pre>
  
  <p>I am getting this data as an output:</p>
  
  <pre><code>Received: 
  b'2018-08-06T17:35:45.0612|power|ON|mode|MANUAL|execution|READY|Xact|215.13|Yact|331.34|Zact|22.16|Xcom|0.00|Ycom|0.00|Zcom|0.00|path_feedrate|0.00|line|0|Block|0|program|Plate 1_imported_dxf.ORD\n'
  b'2018-08-06T17:35:45.0612|comms|NORMAL||||\n2018-08-06T17:35:45.0612|Sspeed|0.00\n'
  </code></pre>
  
  <p>And now I want to convert it to formatted json object so that i can later use it on my html with javascript. I tried to create json object within my while loop by:</p>
  
  <pre><code>import socket
  import json
  
  HOST = "myHOST"
  PORT = myPORT
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((myHOST, myPORT))
      s.sendall(b" ")
      buffer_size = 4096
      print("Received: ")
      while True:
          data = s.recv(buffer_size)
          print(repr(data))
  
          json_obj = json.loads(data)
          print(type(json_obj))
          print(json_obj.get("power").get("mode"))
  s.close()
  </code></pre>
  
  <p>But i cant make it to work. I also tried to use json.dumps but no success.
  Please help guys! I also tried to make a dictionary but no success, getting error: ValueError: too many values to unpack (expected 2).
  Below is the snippet for my dictionary try:</p>
  
  <pre><code>while True:
      data = s.recv(buffer_size).decode("utf-8")
      print(data)
      d = {}
      for pair in data.replace(' ', '').split(', '):
          k, v = pair.split('|')
          d[k] = int(v)
      print(d)
  </code></pre>
  
  <p>I want the answer to have key value pairs.</p>