Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to print JSON escaped from simple quotes #6700

Closed
heniotierra opened this issue Aug 28, 2015 · 12 comments
Closed

Add option to print JSON escaped from simple quotes #6700

heniotierra opened this issue Aug 28, 2015 · 12 comments

Comments

@heniotierra
Copy link

@heniotierra heniotierra commented Aug 28, 2015

Currently, if you want to use the JSON generated by -J option programatically, you're pretty fucked, because if there's a single quote (') in any part of the JSON, that won't allow you to parse the string.
Maybe make the -J option print the JSON with escaped single quotes by default?

@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 28, 2015

Post an example URL that produces an invalid JSON.

@heniotierra
Copy link
Author

@heniotierra heniotierra commented Aug 28, 2015

Actually the JSON is valid, but when I try to parse it in my code, it fails because of unescaped single quotes.

EDIT: removed the code copied by mistake

It falis with the error: "ValueError: Unterminated string starting at ..."

@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 28, 2015

There is no problem then. Just parse it properly with json.loads().

@dstftw dstftw closed this Aug 28, 2015
@heniotierra
Copy link
Author

@heniotierra heniotierra commented Aug 28, 2015

Oh, but I do that. I'm sorry, I copied the wrong part of the code.

ytplst = json.loads(self.ytprocess.match.group(1))              # <- it fails here!

Also, I think it's not possible to escape the thing from inside my code. That must be done during generation.

@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 28, 2015

ValueError: Unterminated string starting at ... has nothing to do with unescaped quotes. Most likely you are trying to load incomplete json that is returned by self.ytprocess.match.group(1).

@heniotierra
Copy link
Author

@heniotierra heniotierra commented Aug 28, 2015

Hum... I thought of that possibility, but I don't understand why that would happen.

Also, to make sure it was a problem with unescaped characters, I copied the generated JSON and parsed it with in the javascript console. Again, it's valid JSON, but in javascript it returns a syntax error.

Javascript console says Uncaught SyntaxError: missing ) after argument list for the code below:
var jota = JSON.parse(' <here comes valid generated json> ')

Now, it works for
var jota = <here comes valid generated json>

However, there's no way to use the json in my code without parsing it... Do you see the problem here? In the first case, if the single quotes are escaped, it will work.

@heniotierra
Copy link
Author

@heniotierra heniotierra commented Aug 28, 2015

Ok, I managed to fix this. Now it works! :)
I'll send a pull request soon.

@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 28, 2015

There is no need in PR since there is no problem.

> str
"{"ab'c": "q'"}"
> JSON.parse(str)
Object { ab'c: "q'" }
@heniotierra
Copy link
Author

@heniotierra heniotierra commented Aug 28, 2015

Hum... It seems to me like you didn't understand what the problem here was.
The pull request is a one line changer which makes the whole thing I'm working on to work.
I don't get why you're resisting this way. I'm sorry, but I doubt you yourself are able to use the generated JSON in some other program of yours if it comes with ' in some string.
I'll send the PR anyway, just so that you can close it. ;) I'm kidding, I mean no offense. But still, don't understand your resistance. Did you even try to use the generated JSON for something useful?

@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 28, 2015

Post the full code you are using to parse it.

@heniotierra
Copy link
Author

@heniotierra heniotierra commented Aug 28, 2015

Ok... this is part of code. Not the full code, cos that's 1339 loc.

            ytcmd = self._YTLAUNCH_PLST_CMD % (mediafile)
            print "      Youtube URL: " + mediafile
            print "      Youtube cmd: " + ytcmd
            self.ytprocess = pexpect.spawn(ytcmd,timeout=90,maxread=17000,searchwindowsize=17000)
            #self.ytprocess.logfile = sys.stdout
            ytplstfound = self.ytprocess.expect([self._YT_REXP,
                                                     pexpect.TIMEOUT,
                                                     pexpect.EOF])
            if ytplstfound == 0:
                ytplst = json.loads(self.ytprocess.match.group(1))
                plst_index = tboplayer.playlist.selected_track_index()
                tboplayer.track_titles_display.delete(plst_index,plst_index)
                tboplayer.playlist.remove(plst_index)
                tboplayer.blank_selected_track()
@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 28, 2015

The full code. How am I supposed to guess what self._YT_REXP equals to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.