Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Not able to read JSON String from the file which has escape chars

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Not able to read JSON String from the file which has escape chars

    Hi All

    I'm facing issue while reading the json string with escape double quote in it which need to be processed by GSON after the extraction. GSON failing with malformed exception as the string conversion not unescaping the \.

    File looks like this (first column 12345 is the key separated by tab with the second column json string):
    12345 {\"orders\": {\"type\": \"metadata\", \"version\": \"b2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"1.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> <OldPositions start=\\\"0\\\" end=\\\"10\\\" /> </saleinfo>\"}}}

    I would be getting the record handler in byte array instead of actual file which i converted to string and then split the record into string array with first element of array as key (12345) and second as Json string. But the json string loads with the escape character.

    Code below:
    byte[] entryData = "file content here in byte array";
    String str = new String(entryData); //converting byte array to string
    String[] lines = str.split("[\\r\\n]+"); //split into records
     
    for(int i=0; i< lines.length; i++) {
    	String[] strArray = lines[i].split("\\t");
    	String key = strArray[0];
    	String jsonStr = new String(strArray[1]);
    	System.out.println("JsonString --- "+jsonStr);
    	//output is {\"orders\": {\"type\": \"metadata\", \"version\": \"b2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"1.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" />    <OldPositions start=\\\"0\\\" end=\\\"10\\\" />  </saleinfo>\"}}}
    	//backslash not escaping
    }

    Following the above, i would be parsing the jsonStr using GSON (cannot use json parser due to requirement) and extract the tag and its' values. As you see each element is JsonObject..

    I got lost on why the unescape not happening converting to string and i cannot use replace due to single and three backslashes in the jsonStr.

    Could you please help me in resolving the issue and let me know if you need more info than provided above.

    Thanks
    kmove
    Last edited by rkmove19; July 8th, 2020 at 02:12 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    If you do not want any \ characters in the String, would the String class's replace method work to remove them?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    Replace backslash not a solution as some of the elements in the jsonStr has \\\ (3) and in future we are not sure. so as such string should able to escape the \ inside the string as like below :

    String json = "{ \"name\": \"Baeldung\", \"java\": true }";
    JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);
    System.out.println(convertedObject.get("name").getAsString());
    // Baeldung

    Above code works as such which is an example taken from https://www.baeldung.com/gson-string-to-jsonobject, but as posted in my question when i read the second column in the delimited file which is string with backslash as above, it is not escaping the backslash.

    Hope i'm clear in explaining my problem.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    not escaping the backslash.
    Can you give a small example of a String that needs to be changed? Not more than one line of characters.
    Show the String's contents:
    before the change
    and after the change
    so we can see what you want to do.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    Input (records from the file, tab delimited):
    12345 {\"orders\": {\"type\": \"metadata\", \"version\": \"b2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"1.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> <OldPositions start=\\\"0\\\" end=\\\"10\\\" /> </saleinfo>\"}}}
    22346 {\"orders\": {\"type\": \"metadata\", \"version\": \"c2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"2.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"20\\\" end=\\\"30\\\" /> <OldPositions start=\\\"1\\\" end=\\\"20\\\" /> </saleinfo>\"}}}

    Extract the Second column json string in the input file
    {\"orders\": {\"type\": \"metadata\", \"version\": \"b2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"1.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> <OldPositions start=\\\"0\\\" end=\\\"10\\\" /> </saleinfo>\"}}}
    {\"orders\": {\"type\": \"metadata\", \"version\": \"c2f\", \"result\": null},\"saleinfo\": {\"type\": \"metadata\", \"version\": \"2.0\", \"result\": {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"20\\\" end=\\\"30\\\" /> <OldPositions start=\\\"1\\\" end=\\\"20\\\" /> </saleinfo>\"}}}

    Input expected for parsing JSON using GSON :
    {"orders": {"type": "metadata", "version": "b2f", "result": null},"saleinfo": {"type": "metadata", "version": "1.0", "result": {"saleinfo": "<saleinfo> <NewPositions start=\"10\" end=\"20\" /> <OldPositions start=\"0\" end=\"10\" /> </saleinfo>"}}}
    {"orders": {"type": "metadata", "version": "c2f", "result": null},"saleinfo": {"type": "metadata", "version": "2.0", "result": {"saleinfo": "<saleinfo> <NewPositions start=\"20\" end=\"30\" /> <OldPositions start=\"1\" end=\"20\" /> </saleinfo>"}}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    There are too many lines for easy comparison. Can you make a smaller sample?

    Do these lines show it:
    input: {\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> </saleinfo>\"}
    output: {"saleinfo": "<saleinfo> <NewPositions start=\"10\" end=\"20\" /> </saleinfo>"}
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    Sure. It will do.

    Thanks

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    I do not know if there are packages that will do that. There could be, I have no idea if any exist.

    Look what does the compiler does with that line:
    System.out.println(
    "{\"saleinfo\": \"<saleinfo> <NewPositions start=\\\"10\\\" end=\\\"20\\\" /> </saleinfo>\"}");
    // {"saleinfo": "<saleinfo> <NewPositions start=\"10\" end=\"20\" /> </saleinfo>"}


    If you want to write your own code, you need to start with a description of what is to be done before trying to write any code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    rkmove19 (July 8th, 2020)

  10. #9
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    Thanks Norm.

    If i keep the string in System.out.println, it prints correctly as expected as you shown.

    But when i directly read the string from the file and place it in variable for further processing, backslash not going away (called unescaped).

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    it prints correctly
    That is AFTER the compiler has done the escaping for the \s in the source line.
    when i directly read the string from the file
    I think what you want is code that works like the compiler.

    Take a look at this package: org.apache.commons.text.StringEscapeUtils
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    rkmove19 (July 8th, 2020)

  13. #11
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Not able to read JSON String from the file which has escape chars

    Sure. i will check this package.

    Thanks

Similar Threads

  1. Replies: 3
    Last Post: June 26th, 2013, 02:12 AM
  2. How to read a String .dat file into an array
    By Wallatrix in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 5th, 2013, 02:55 PM
  3. Fastest way to read and search a string in a large file using java
    By patilsn_jay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 09:29 AM
  4. HW-how to read a string from a text file
    By yaboibangz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 09:34 AM
  5. [SOLVED] tokenizing a string with escape sequence
    By nosxlimit in forum Java Theory & Questions
    Replies: 9
    Last Post: July 26th, 2012, 12:30 AM

Tags for this Thread