none
Why do I have to tostring() a string property before I can parse it to json?

    Question

  • Hey,

    I'm experiencing a weird behavior. We log JSON strings in application insights custom dimensions so we can have structured details about an exception. When we try to query that dynamic data, we have this strange behavior that I am trying to understand.

    In the following query, you can see that I have to run tostring() on a string property before I can parse it to a dictionary. Read the projected property names to understand.

    exceptions
    | project
        thisWillReturnString = gettype(customDimensions.ExceptionDetail),
        thisWillReturnStringAgain = gettype(parse_json(customDimensions.ExceptionDetail)),
        thisWillReturnDictionary = gettype(parse_json(tostring(customDimensions.ExceptionDetail)))

    Is there an explanation why I have to do this or is there a better to achieve parsing a JSON string to dictionary?

    Friday, April 5, 2019 2:00 PM

All replies

  • Hi Pluc15,

    Based on the below sample, it does show that its returning the same gettype by each of the format you have listed. 

    print customDimensionJsonString= dynamic({"name":"Alan", "age":21, "address":{"street":432,"postcode":"JLK32P"}})
    | extend thiswillReturnString = gettype(customDimensionJsonString.name) 
    | extend thiswillReturnStringAgain = gettype(parse_json(customDimensionJsonString.name)) 
    | extend thiswillReturnStringOnly = gettype(parse_json(tostring(customDimensionJsonString.name) ) ) 

    print customDimensionJsonString= dynamic({"name":"Alan", "age":21, "address":{"street":432,"postcode":"JLK32P"}})
    | extend thiswillReturnlong = gettype(customDimensionJsonString.age) 
    | extend thiswillReturnlongAgain = gettype(parse_json(customDimensionJsonString.age)) 
    | extend thiswillReturnlongonly = gettype(parse_json(tostring(customDimensionJsonString.age) ) ) 


    print customDimensionJsonString= dynamic({"name":"Alan", "age":21, "address":{"street":432,"postcode":"JLK32P"}})
    | extend thiswillReturndictionary = gettype(customDimensionJsonString.address) 
    | extend thiswillReturndictionaryAgain = gettype(parse_json(customDimensionJsonString.address)) 
    | extend thiswillReturndictionaryonly = gettype(parse_json(tostring(customDimensionJsonString.address) ) ) 


    If its not the same behavior you see with your customDimension, can you please share one sample of the exception from your dataset. Make sure you mask any of the sensitive information you might have. 

    Additional documentation reference - 

    ParseJson

    dynamic

    Hope the above information helps, please revert back for any queries.

     


    Monday, April 8, 2019 4:28 AM
    Owner
  • So I don't know what my data is like, but I added some "<g class="gr_ gr_34 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="34" id="34">tostring</g>" to my parse <g class="gr_ gr_46 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="46" id="46">json</g> queries and it worked, so THANK YOU for this, even if it's a "Why do I have to do it" since it helped me get what I needed!
    Monday, April 8, 2019 3:06 PM