Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

Editing a .json property value (adding in json escape \ chars to property value)?

Hi,

 

I have a REST GET request that returns the following .json from the CRM (below .json is for 1 record - the GET retrieves multiple records)

{
	"@odata.type": "#Microsoft.Dynamics.CRM.whatever",
	"@odata.id": "https://XXXX.dynamics.com/api/data/v8.2/whatever(a038fae4-ef43-e811-811f-5065f38b03d1)",
	"@odata.etag": "W/"3859692"",
	"@odata.editLink": "whatever(a038fae4-ef43-e811-811f-5065f38b03d1)",
	"activityid@odata.type": "#Guid",
	"activityid": "a038fae4-ef43-e811-811f-5065f38b03d1"
}

 

Now I need to grab the attribute values and submit them in a subsequent POST request via a property transfer - I have a property created for each .json attribute.

 

The problem is the "@odata.etag" value.  I need to alter thee value because it has double quote characters (e.g. ") in the value - so I need to escape the double quote chars using a \ symbol

 

So "W/"3859692"" needs to be altered to "W/\"3859692\"" 

 

 

I'm at a bit of a loss how to do this - the value is currently stored in a property as "W/"3859692"" - I just have no idea how I'd go about inserting the escape characters into the property value.

 

Any ideas?

 

As always - thanks to all for any advice, guidance, tips, help that anyone can give me!

 

Cheers,

 

richie

1 Reply

  • richie's avatar
    richie
    Community Hero

    Hey!

     

    Ok- I've been playing with a script I had before for replacing CDATA handler tags from an XML response - I thought I'd be able to use this but I'm struggling - my script is as follows at the moment (I'm using the jsonSlurper cos I'm struggling with the jsonUtil at moment - if I can get it to work with the jsonSlurper on v2.2.0 - I'll then get it working with the jsonUtil on v2.3.0)  

     

    Anyway - please see below:

     

    import net.sf.json.groovy.*
    import groovy.json.JsonSlurper
    
    //grab the @odata.etag0 property
    def Jresponse = context.expand( '${Properties 1#@odata.etag0}' )
    
    //parse the Jresponse variable which contains the @odata.etag0 property
    def json = new JsonSlurper().parseText Jresponse
    
    // Insert the escape chars ('\') into the Properties value
    response = Jresponse.replaceAll( "W/"", "W/\"" )
    response = Jresponse.replaceAll( """", "\""" ) <<---struggling here

     

    Now as stated in my first post - the value saved to the property is W/"123456" - I need to alter this property value to W/\"123456\"

     

    I can insert the \ escape character after the / character - but I don't know the approach i should use to insert a \ before the last double quote (") - there's nothing unique for me to do a find a replace against to insert the \ escape char before the final double quote.

     

    The original property value will always be in the format W/"nnnnnn" (where n = integer) - so I was wondering if there was some sort of substring function that could allow me to count and insert a \ char after the 9th char but before the 10th char?

     

    I'm not a coder - but I'm really enjoying playing with groovy at the moment!

     

    Anyway - I'd welcome any thought anyones has in regards to inserts the final \ escape character

     

    Cheers,

     

    richie