Forum Discussion

sdhamblet's avatar
sdhamblet
Copper Contributor
Mar 31, 2025

Embedded Json being interpretted incorrectly

I created an API project in .NET 6.0 with an endpoint that accepts a Json request body with Json embedded in one of the attributes of the request.  The problem I'm having is that within the embedded Json, one of the values has an escaped double quotes right before an open square bracket, and when the embedded Json is received by the endpoint, the escaped double quote is missing.  

Here is an example of how the end point is being called:POST /DataRequest/MyDataBody:{

    "HostName": "my-host-01",

    "Job": "some_job",

    "JobData": "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\";\"[val3] foo\"}"

}

The issue I see with JobData is that the \"[ after the attr3 definition gets immediately translated to just [.  When I say Immediately, I mean that I have a break point on the very first line of the endpoint code and the escaped double quote is already removed from the JobData attribute.So, when I'm looking at the value that was received by the end point, it looks like this:"JobData": "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\";[val3] foo\"}"

This ends up being invalid Json and causes errors further down in the code.  I tried to do some research on this, but it's pretty hard to google something like this and get any sort of answer.  I did some experimenting and found that these combinations:\"attr3\":\"\"[val3] foo\"     produces    "attr3":"[val3] foo"\"attr3\":\" [val3] foo\"       produces    "attr3":" [val3] foo"      (note, there's a space between the escaped double quote and the bracket)It seems like something in the framework is incorrectly assuming that the open bracket is starting a list even though it's got an escaped double quote in front of it.  I also found that the sequence ]\" does the same thing on the other end where the escaped double quotes gets dropped in the string.

Is \"[ and ]\" a special case escape that I'm just not aware of?  Has anyone else run across this?  We've kind of worked around the issue using the space between the escaped double quote and the open bracket, but I was hoping to find out why this happens.

If I'm posting this in the wrong place, please let me know where I should post this.

 

2 Replies

  • sdhamblet's avatar
    sdhamblet
    Copper Contributor

    Nevermind, I found out where my problem was.  My model was doing some clean up on the embedded text and was inadvertently removing these character sequences.

  • sdhamblet's avatar
    sdhamblet
    Copper Contributor

    Nevermind, this issue was induced with some other code I added to try to clean up a bunch of unexpected stuff in the embedded string.  I have my answer.

Resources