[SOLVED]json help

Posted on
Wed Jan 29, 2014 4:19 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

[SOLVED]json help

I have a "simple" py- json question -- its not indigo, but I am using it for indigo..


Code: Select all
test ='[{"a":"1","b":"2"},{"c":"5","d":"6"}]'
jst = json.loads(test)
jst
[{u'a': u'1', u'b': u'2'}, {u'c': u'5', u'd': u'6'}]

works


the following does not work:
Code: Select all
test ="[{'a':'1','b':'2'},{'c':'5','d':'6'}]"
jst = json.loads(test)

gives
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 3 (char 2)



difference is in the quotes.. I have a file that has single quotes in the data

Any idea? is there parameter to flip the quotes in json?

Posted on
Wed Jan 29, 2014 5:37 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: json help

with

test2 = test.replace("'",'"') ## replace ' with "

json.loads(test2)

it works


really do not understand this

why is " different from ' in a string everything I read states it is the same!1

Posted on
Wed Jan 29, 2014 8:22 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: json help

The single and double quotes are, to my knowledge, interchangeable in the Python language... but JSON is not Python and adheres to a stricter ruleset such that identifiers must use double quotes for strings. I presume this is because it is designed for use by any programming language and many (most?) DO make a distinction between the two quotes; the double quote is more commonly used as a string identifier.

Posted on
Wed Jan 29, 2014 8:25 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: json help

ok, but why do programs then create a file that is not compatible?ie can't not be read by json? .. just astonished ..

Posted on
Wed Jan 29, 2014 9:04 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: json help

Lazy? Didn't know better?

Why did Microsoft create a non-standards compliant browser that left us cursing every time we try to make pages look the same in all browsers? (sorry, annoyance on that issue from all day today at work :))

What program - is it a well known one or just something someone threw together (as that might explain it)?

Posted on
Wed Jan 29, 2014 9:49 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: json help

fing with json output option :

fing -s 192.168.1.0/24 -o json,/Users/........./output

creates:

[{'Address':'192.168.1.0','Family':'INET4','FirewallDetected':'false','Hostname':'','CustomName':'','ScanResult':'host unreachable','Services':[]},{'Address':'192.168.1.1','Family':'INET4','FirewallDetected':'false','H................},]

also some "dead" "," are in there like: },] the "," kills it too. They have to be removed apparently you can't have a , without a name following it

if "f" is the text string read from file:
f = open(fingServicesFileName,"r").read()
then this makes it work:
1. replace ' with "
2. drop "," before }
3. drop last "," before ]

theServices=json.loads(f.replace("'",'"').replace(",},","},").replace("},]","}]"))

took me the better part of today.

Karl

Posted on
Wed Jan 29, 2014 10:10 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: json help

[{'Address':'192.168.1.0','Family':'INET4','FirewallDetected':'false','Hostname':'','CustomName':'','ScanResult':'host unreachable','Services':[]},{'Address':'192.168.1.1','Family':'INET4','FirewallDetected':'false','H................},]


That is pretty bad all around - I don't believe that would pass any JSON validator; the straight brackets are essentially an array, so a comma with nothing following it would indicate that there should be another array element, but nothing follows.

Adam

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests