-
Notifications
You must be signed in to change notification settings - Fork 76
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
Even the simplest json can not be parsed on FreeBSD or OS X #10
Comments
Thank you. I can reproduce it using the FreeBSD-11.2-STABLE VM. I will be able to take a deeper look in a few days because I'm currently travelling. |
JSON.awk fails to parse the example you provided because the FreeBSD awk implementation behaves differently from Unlike the other awk flavors that I mentioned above, FreeBSD awk seems not to support assigning a regular expression to the built-in Specifically, for the json file you provided (x.json), FreeBSD awk breaks the input record on the first "n" in "name" because "n" is the first character of Possible Work-arounds If you don't want to change JSON.awk source code, you can install gawk (recommended), mawk (fastest) or busybox awk (smallest, slowest) and run JSON.awk with that. Versions tested on FreeBSD-11.2-STABLE
If you need to use FreeBSD awk, knowing its limitation, you need to change the source code. Set the record separator to a single character that doesn't appear anywhere in the input file -- you must know that in advance. For instance, you could set RS = "\b" Note that theoretically any Unicode codepoint is valid input (with proper escaping) in Json, so you might find it difficult to determine a character that is guaranteed not to exist across all possible input streams. However, practically most input data will not include certain control characters or high-bit characters or higher codepoints. |
As I understand now, what the problem is, I can make the decision not to try harder with JSON.awk. I have this tool called mulle-fetch where I'd like to start parsing github API. It needs to work with a standard awk, for portability reasons. So I will use JSON.sh instead, for less hassle. Thanks for the detailed reply, it was very much appreciated. |
OK, thank you for your report, and good luck with your project! |
Changing
|
Thank you for your comment. What RS string did you use? What platform, OS version, awk flavor and version, input data produced the above error message? |
I tried a few, '\b', '\t', '\030'. Btw those aren't allowed in JSON unescaped so the The macOS (and FreeBSD) awk is an old version of onetrueawk. Strangely, the latest version of onetrueawk works fine. |
@mohd-akram can you please test 7550234 and tell me if the old onetrueawk version works now? Thank you. |
After changing RS, It works! Thanks! Perhaps you can change RS to |
@mohd-akram, thank you for testing.
Hmm, it's a good idea but probably I won't change it because of this. Even though JSON.awk doesn't support streaming JSON now I don't want to preclude this avenue for the future. On the other hand, your suggestion made me think that I could change RS to a string that starts with a control character (not RS or \t, \f, \r, \n, space). This change would make no different on platforms other than MacOS and FreeBSD running |
Good point. I used your awesome project for building an awk for JSON. I patched it slightly to use |
@mohd-akram, your jawk is very cool! May I link to it from my "Projects known to use JSON.awk" README section? I added a comment to your patch file, if you would please answer. One more question; have you considered using JSON.awk's callback interface for your project instead of a full patch? I'm not suggesting you shouldn't use a patch, just curious to get some feedback to improve JSON.awk's callback interface. |
Thank you! It would be great to have it linked! I tried the callback interface briefly and I noticed that I had to implement quite a few callbacks to get it working, and I really only needed to nudge the output a bit so writing a patch was faster, and I could prototype quickly. Now that I've mostly decided on the interface and have a bunch of tests written, it might be worth looking into it again. I think most of my changes can be done using callbacks. One thing that would probably require some minor changes to JSON.awk is allowing a stream of multiple JSON objects. It would also probably be more complicated for me to get the length of the array, and the list of keys in an object. Btw the link to "library" and |
Aimed at old "onetrueawk" versions as the preceding commit. See #10 for details.
produces
This is on FreeBSD with
awk version 20121220 (FreeBSD)
as well as on Darwin withawk version 20070501
.The text was updated successfully, but these errors were encountered: