v.db.select: Add -e flag (escape newlines and backslashes) and -j flag (JSON output)#476
Conversation
vs. I think because of escaped backslashes and rare (hopefully) cases of newlines in records, it's better to keep the |
|
Hard to say, but I'm saying yes to Why this is hard to decide or to do is, I think, caused by a more general issue and that is lack of definition for these output formats (of GRASS modules); similarly to the discussion in #325. Perhaps v.report is for viewing rather than subsequent parsing, then escaping new lines by default would make sense, but escaping backslashes would not. If it is for querying, a commonly used format, even if it is just a well-defined CSV, would be needed for smooth subsequent parsing. That's a decision we have already made for complex output of v.what. See how this nicely plays out in this random example of parsing its output with a standard tools (here JSON reading in Ruby and Python): |
I agree to keep the default behavior.
Escaping only newlines is problematic as shown in the above example ( v.report map=tmp option=area | sed 's/\\\\/\x0/g; s/\\n/\n/g; s/\x0/\\/g'
IMO, JSON is less human-readable, but it would be great to implement the JSON format in |
|
Note: JSON support is currently being worked on in OSGeo/grass-addons#137 (in Python, though). |
|
For our record, the pipe character ( db.execute sql="update tmp set name='BREAK|ME AGAIN' where cat=1" |
|
Hi Huidae, while you are at the newline issue, would you mind looking at r.info as well? See: |
Not sure how I can create a separate PR that depends on this one, which is not committed yet. I tried to create a new PR, but that contained this commit as well. Anyway, this PR now includes g.copy --o vect=boundary_county,tmp
db.execute sql="update tmp set name='BREAK' || char(10) || '"'"'"ME''\nPLEASE' where cat=1"
v.db.select -j map=tmp |
python3 -c 'import sys, json; recs=json.loads(sys.stdin.read()); print(recs[0]["NAME"])'outputs |
Hi Stefan, How can I reproduce it in the NC sample dataset? |
You could do: |
echo $comments
r.proj input=ned03arcsec location=northcarolina_latlong mapset=helena output=elev_ned10m method=cubic resolution=10BREAKMEYou want to create a new issue ticket? |
Just create the new branch from master, not from the branch for this PR: |
Wouldn't that create conflicts? Can we merge both PRs with no conflicts? or PR 1 first, then PR 2? |
wenzeslaus
left a comment
There was a problem hiding this comment.
I'm sorry to request more after suggesting the JSON, but since the JSON is generated manually, it would be good to at least have a test which checks that the generated text is a valid JSON by loading it as JSON into Python.
I think you're talking about |
What is there is great. Additional test which would be worth it (and what I meant originally) is simply loading it into Python using json package ( |
wenzeslaus
left a comment
There was a problem hiding this comment.
I like what you did with the rest of the code. Thanks.
Yes, I thought about that. Just loading the output to validate the JSON format only, but the same test script does depend on the same data set. That's why I decided to check the validity of the data as well. Let me do something about this. |
Done in f493ac1 |
* v.report: Fix newline issue * v.db.select -n => -e (#476)
Issue
If there are newline characters in a column,
v.db.selectwill print multiple lines per record, which breaksv.report(#477). I found this issue in the GADM 3.6 dataset.How to reproduce it in the NC sample dataset
will print two lines:
What does this PR do?
will print
Thoughts
Maybe, we should make -n flag default?