0

I am getting a big chunk of Json-like data from a web service. The data has the same format as Json except:

  1. The name identifier is not in quotes.
  2. The value is in single quotes.

A sample would be:

[{ID:0,N:'3ergy',SIP:'',NC:'502',R:'',....

Is this a well-known format? Is there a Python tool that can convert it to a list?

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • 2
    Take a look at http://stackoverflow.com/questions/9104930/is-there-any-way-to-make-simplejson-less-strict – DrC Nov 18 '14 at 05:42

2 Answers2

1

As DrC suggested i have been able to parse the line you supplied with pyyaml .

import yaml

data = "[{ID:0,N:'3ergy',SIP:'',NC:'502',R:''}]"
parsed_data = yaml.load( ": ".join(data.split(":")))[0]
# {'SIP': '', 'R': '', 'NC': '502', 'ID': 0, 'N': '3ergy'}

parsed_data.get("NC")
# '502'
Alexander
  • 12,424
  • 5
  • 59
  • 76
  • Thanks. YAML does the trick, but for a 240kB string it took more than 3 minutes CPU maxed out on a Raspberry Pi. In the end I wrote my own code making all the assumptions about the data (no nesting, no commas in the value field, etc) and it took less than 1s for the same input string. – Old Geezer Nov 18 '14 at 09:20
  • 1
    Please share your solution, so future visitors can benefit from it – Alexander Nov 18 '14 at 09:51
1

I hesitate to share my solution as it is pretty much hardcoded to exploit the characteristics of this particular piece of input data. But acceding to Alexander's request, here goes:

# source data is stocks="[{ID:0,N:'3ergy',SIP:'',NC:'502',R:'',...},
#                         {ID:1,N:'aaaaa',SIP:'',NC:'nnn',R:'nn',...}, .... ]"
Stocks = [] #parsed result
i = 1
while i>0:
   end = stocks.find('}',i)
   stock = stocks[i+1:end]
   parts = stock.split(',')
   Stock = {}
   for part in parts:
      key,value=part.split(':')
      Stock[key] = value.strip("'"))
   Stocks.append(Stock)
   i = stocks.find('{',end)
Old Geezer
  • 14,854
  • 31
  • 111
  • 198