Parsing JSON with groovy LAX parser gives wrong keys












0














I have some config files in JSON format, and I need to parse them in a program written in groovy. Since the files should be human readable, I'd like to use the LAX syntax (and parser) so that I do not need to wrap every key and value in quotation marks and also can use comments.



But somehow, when I parse a JSON file, I get wrong keys in the resulting map.



Here is an example.



The JSON file (I saved it as a.json) is



{
key1: aaa,
key2: bbb
}


The program to parse it is



package test.json;

import groovy.json.JsonParserType;
import groovy.json.JsonSlurper;

public class JsonParser {

static void main(String args) {
JsonSlurper slurper = new JsonSlurper().setType(JsonParserType.LAX)
def config = slurper.parse(new File('a.json'))
println "key1: ${config.key1}"
println "key2: ${config.key2}"
for (e in config) {
println "*** '${e.key}' (${e.key.class}) = '${e.value}' (${e.value.class})"
}
}

}


When I execute the program, I get the following output:



key1: aaa
key2: null
*** 'key1' (class java.lang.String) = 'aaa' (class java.lang.String)
*** '
key2' (class java.lang.String) = 'bbb
' (class java.lang.String)


You see that the second key is not parsed as 'key2' but includes spaces and a line break.



How can I make the parser to remove the spaces from the keys and values so that I get 'key2'='bbb' (as I'd expect for the file above)?



If I enclose each key and value in double quotation marks, everything is parsed as expected, i.e. I get 'key2'='bbb'. But I thought with the LAX parser I'd not need them.










share|improve this question






















  • I couldn't reproduce the problem you have shown in the question. I took the file content you have pasted and the code you added - Groovy 2.4, 2.5 and 3.0-alpha executed as expected. I would suggest checking the input file - maybe there is some character that breaks key2 to the next line. Try the same sample but with JSON as a string variable passed to slurper.parseText(). And check the input file character encoding. LAX parser works as you described, it does not add any new line character. If you see it, it means it exists in the input file somehow.
    – Szymon Stepniak
    Nov 21 '18 at 23:07










  • Thank you for the suggestion. I tried it (parsing the text from a string variable) and it worked. Then I switched back to the file, but placed both values on the same line -- and it worked. So I assumed it should somehow depend on newlines. I changed the newline to UNIX style (LF only) -- and it works! I don't understand why it doesn't work with Windows style newlines (CR, LF). For the reference: I use groovy 2.4.15, and I'm on a Windows platform.
    – fml2
    Nov 22 '18 at 6:08


















0














I have some config files in JSON format, and I need to parse them in a program written in groovy. Since the files should be human readable, I'd like to use the LAX syntax (and parser) so that I do not need to wrap every key and value in quotation marks and also can use comments.



But somehow, when I parse a JSON file, I get wrong keys in the resulting map.



Here is an example.



The JSON file (I saved it as a.json) is



{
key1: aaa,
key2: bbb
}


The program to parse it is



package test.json;

import groovy.json.JsonParserType;
import groovy.json.JsonSlurper;

public class JsonParser {

static void main(String args) {
JsonSlurper slurper = new JsonSlurper().setType(JsonParserType.LAX)
def config = slurper.parse(new File('a.json'))
println "key1: ${config.key1}"
println "key2: ${config.key2}"
for (e in config) {
println "*** '${e.key}' (${e.key.class}) = '${e.value}' (${e.value.class})"
}
}

}


When I execute the program, I get the following output:



key1: aaa
key2: null
*** 'key1' (class java.lang.String) = 'aaa' (class java.lang.String)
*** '
key2' (class java.lang.String) = 'bbb
' (class java.lang.String)


You see that the second key is not parsed as 'key2' but includes spaces and a line break.



How can I make the parser to remove the spaces from the keys and values so that I get 'key2'='bbb' (as I'd expect for the file above)?



If I enclose each key and value in double quotation marks, everything is parsed as expected, i.e. I get 'key2'='bbb'. But I thought with the LAX parser I'd not need them.










share|improve this question






















  • I couldn't reproduce the problem you have shown in the question. I took the file content you have pasted and the code you added - Groovy 2.4, 2.5 and 3.0-alpha executed as expected. I would suggest checking the input file - maybe there is some character that breaks key2 to the next line. Try the same sample but with JSON as a string variable passed to slurper.parseText(). And check the input file character encoding. LAX parser works as you described, it does not add any new line character. If you see it, it means it exists in the input file somehow.
    – Szymon Stepniak
    Nov 21 '18 at 23:07










  • Thank you for the suggestion. I tried it (parsing the text from a string variable) and it worked. Then I switched back to the file, but placed both values on the same line -- and it worked. So I assumed it should somehow depend on newlines. I changed the newline to UNIX style (LF only) -- and it works! I don't understand why it doesn't work with Windows style newlines (CR, LF). For the reference: I use groovy 2.4.15, and I'm on a Windows platform.
    – fml2
    Nov 22 '18 at 6:08
















0












0








0







I have some config files in JSON format, and I need to parse them in a program written in groovy. Since the files should be human readable, I'd like to use the LAX syntax (and parser) so that I do not need to wrap every key and value in quotation marks and also can use comments.



But somehow, when I parse a JSON file, I get wrong keys in the resulting map.



Here is an example.



The JSON file (I saved it as a.json) is



{
key1: aaa,
key2: bbb
}


The program to parse it is



package test.json;

import groovy.json.JsonParserType;
import groovy.json.JsonSlurper;

public class JsonParser {

static void main(String args) {
JsonSlurper slurper = new JsonSlurper().setType(JsonParserType.LAX)
def config = slurper.parse(new File('a.json'))
println "key1: ${config.key1}"
println "key2: ${config.key2}"
for (e in config) {
println "*** '${e.key}' (${e.key.class}) = '${e.value}' (${e.value.class})"
}
}

}


When I execute the program, I get the following output:



key1: aaa
key2: null
*** 'key1' (class java.lang.String) = 'aaa' (class java.lang.String)
*** '
key2' (class java.lang.String) = 'bbb
' (class java.lang.String)


You see that the second key is not parsed as 'key2' but includes spaces and a line break.



How can I make the parser to remove the spaces from the keys and values so that I get 'key2'='bbb' (as I'd expect for the file above)?



If I enclose each key and value in double quotation marks, everything is parsed as expected, i.e. I get 'key2'='bbb'. But I thought with the LAX parser I'd not need them.










share|improve this question













I have some config files in JSON format, and I need to parse them in a program written in groovy. Since the files should be human readable, I'd like to use the LAX syntax (and parser) so that I do not need to wrap every key and value in quotation marks and also can use comments.



But somehow, when I parse a JSON file, I get wrong keys in the resulting map.



Here is an example.



The JSON file (I saved it as a.json) is



{
key1: aaa,
key2: bbb
}


The program to parse it is



package test.json;

import groovy.json.JsonParserType;
import groovy.json.JsonSlurper;

public class JsonParser {

static void main(String args) {
JsonSlurper slurper = new JsonSlurper().setType(JsonParserType.LAX)
def config = slurper.parse(new File('a.json'))
println "key1: ${config.key1}"
println "key2: ${config.key2}"
for (e in config) {
println "*** '${e.key}' (${e.key.class}) = '${e.value}' (${e.value.class})"
}
}

}


When I execute the program, I get the following output:



key1: aaa
key2: null
*** 'key1' (class java.lang.String) = 'aaa' (class java.lang.String)
*** '
key2' (class java.lang.String) = 'bbb
' (class java.lang.String)


You see that the second key is not parsed as 'key2' but includes spaces and a line break.



How can I make the parser to remove the spaces from the keys and values so that I get 'key2'='bbb' (as I'd expect for the file above)?



If I enclose each key and value in double quotation marks, everything is parsed as expected, i.e. I get 'key2'='bbb'. But I thought with the LAX parser I'd not need them.







json parsing groovy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 22:12









fml2fml2

465




465












  • I couldn't reproduce the problem you have shown in the question. I took the file content you have pasted and the code you added - Groovy 2.4, 2.5 and 3.0-alpha executed as expected. I would suggest checking the input file - maybe there is some character that breaks key2 to the next line. Try the same sample but with JSON as a string variable passed to slurper.parseText(). And check the input file character encoding. LAX parser works as you described, it does not add any new line character. If you see it, it means it exists in the input file somehow.
    – Szymon Stepniak
    Nov 21 '18 at 23:07










  • Thank you for the suggestion. I tried it (parsing the text from a string variable) and it worked. Then I switched back to the file, but placed both values on the same line -- and it worked. So I assumed it should somehow depend on newlines. I changed the newline to UNIX style (LF only) -- and it works! I don't understand why it doesn't work with Windows style newlines (CR, LF). For the reference: I use groovy 2.4.15, and I'm on a Windows platform.
    – fml2
    Nov 22 '18 at 6:08




















  • I couldn't reproduce the problem you have shown in the question. I took the file content you have pasted and the code you added - Groovy 2.4, 2.5 and 3.0-alpha executed as expected. I would suggest checking the input file - maybe there is some character that breaks key2 to the next line. Try the same sample but with JSON as a string variable passed to slurper.parseText(). And check the input file character encoding. LAX parser works as you described, it does not add any new line character. If you see it, it means it exists in the input file somehow.
    – Szymon Stepniak
    Nov 21 '18 at 23:07










  • Thank you for the suggestion. I tried it (parsing the text from a string variable) and it worked. Then I switched back to the file, but placed both values on the same line -- and it worked. So I assumed it should somehow depend on newlines. I changed the newline to UNIX style (LF only) -- and it works! I don't understand why it doesn't work with Windows style newlines (CR, LF). For the reference: I use groovy 2.4.15, and I'm on a Windows platform.
    – fml2
    Nov 22 '18 at 6:08


















I couldn't reproduce the problem you have shown in the question. I took the file content you have pasted and the code you added - Groovy 2.4, 2.5 and 3.0-alpha executed as expected. I would suggest checking the input file - maybe there is some character that breaks key2 to the next line. Try the same sample but with JSON as a string variable passed to slurper.parseText(). And check the input file character encoding. LAX parser works as you described, it does not add any new line character. If you see it, it means it exists in the input file somehow.
– Szymon Stepniak
Nov 21 '18 at 23:07




I couldn't reproduce the problem you have shown in the question. I took the file content you have pasted and the code you added - Groovy 2.4, 2.5 and 3.0-alpha executed as expected. I would suggest checking the input file - maybe there is some character that breaks key2 to the next line. Try the same sample but with JSON as a string variable passed to slurper.parseText(). And check the input file character encoding. LAX parser works as you described, it does not add any new line character. If you see it, it means it exists in the input file somehow.
– Szymon Stepniak
Nov 21 '18 at 23:07












Thank you for the suggestion. I tried it (parsing the text from a string variable) and it worked. Then I switched back to the file, but placed both values on the same line -- and it worked. So I assumed it should somehow depend on newlines. I changed the newline to UNIX style (LF only) -- and it works! I don't understand why it doesn't work with Windows style newlines (CR, LF). For the reference: I use groovy 2.4.15, and I'm on a Windows platform.
– fml2
Nov 22 '18 at 6:08






Thank you for the suggestion. I tried it (parsing the text from a string variable) and it worked. Then I switched back to the file, but placed both values on the same line -- and it worked. So I assumed it should somehow depend on newlines. I changed the newline to UNIX style (LF only) -- and it works! I don't understand why it doesn't work with Windows style newlines (CR, LF). For the reference: I use groovy 2.4.15, and I'm on a Windows platform.
– fml2
Nov 22 '18 at 6:08














1 Answer
1






active

oldest

votes


















0














This works for me now, the clue is to use the normalize() method:



def json = new File('a.json').text.normalize()
def config = slurper.parseText(json)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421172%2fparsing-json-with-groovy-lax-parser-gives-wrong-keys%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This works for me now, the clue is to use the normalize() method:



    def json = new File('a.json').text.normalize()
    def config = slurper.parseText(json)





    share|improve this answer


























      0














      This works for me now, the clue is to use the normalize() method:



      def json = new File('a.json').text.normalize()
      def config = slurper.parseText(json)





      share|improve this answer
























        0












        0








        0






        This works for me now, the clue is to use the normalize() method:



        def json = new File('a.json').text.normalize()
        def config = slurper.parseText(json)





        share|improve this answer












        This works for me now, the clue is to use the normalize() method:



        def json = new File('a.json').text.normalize()
        def config = slurper.parseText(json)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 20:25









        fml2fml2

        465




        465






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421172%2fparsing-json-with-groovy-lax-parser-gives-wrong-keys%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'