1
$\begingroup$

I'm trying to remove "" from the pattern "true" in the string

str1 = "{\"query\":{\"departmentnumber\":\"353B\",\"isactive\":\"true\"},\"\
fields\":{\"sn\":\"true\",\"givenname\":\"true\",\"uid\":\"true\",\"\
departmentnumber\":\"true\",\"_id\":\"false\"},\"limit\":200,\"sort\":\
[{\"departmentnumber\":\"asc\"},{\"sn\":\"asc\"},{\"givenname\":\"asc\
\"}]}"

I tried using

StringReplace[str1, "true" -> true]

But got this instead

"{\"query\":{\"departmentnumber\":\"353B\",\"isactive\":\"" ~~ true \
~~ "\"},\"fields\":{\"sn\":\"" ~~ true ~~ "\",\"givenname\":\"" ~~ \
true ~~ "\",\"uid\":\"" ~~ true ~~ "\",\"departmentnumber\":\"" ~~ \
true ~~ "\",\"_id\":\"false\"},\"limit\":200,\"sort\":[{\"\
departmentnumber\":\"asc\"},{\"sn\":\"asc\"},{\"givenname\":\"asc\"}]}\
"

What am I doing wrong?

$\endgroup$
2
  • $\begingroup$ I don't understand. Your output seems to indicate that it worked: what did you want instead? $\endgroup$
    – MarcoB
    Dec 9 '19 at 21:07
  • $\begingroup$ I just want true without any of the ~~ or "" surrounding it. $\endgroup$ Dec 9 '19 at 21:08
3
$\begingroup$

The expression "true" represents the string with content true, not "true". To get the string including the quotes, you need to add them to the string, which means it should be "\"true\"" (the \ is to escape the ", so that it is not confused with the end of the string). And you want to replace it with the string containing true, that is "true". Put together, this gives:

str1 = "{\"query\":{\"departmentnumber\":\"353B\",\"isactive\":\"true\"},\"\
fields\":{\"sn\":\"true\",\"givenname\":\"true\",\"uid\":\"true\",\"\
departmentnumber\":\"true\",\"_id\":\"false\"},\"limit\":200,\"sort\":\
[{\"departmentnumber\":\"asc\"},{\"sn\":\"asc\"},{\"givenname\":\"asc\
\"}]}"

StringReplace[str1, "\"true\"" -> "true"]
(* "{\"query\":{\"departmentnumber\":\"353B\",\"isactive\":true},
  \"fields\":{\"sn\":true,\"givenname\":true,\"uid\":true,\"departmentnumber\":true,
     \"_id\":\"false\"},
  \"limit\":200,
  \"sort\":[{\"departmentnumber\":\"asc\"},{\"sn\":\"asc\"},{\"givenname\":\"asc\"}]}" *)
$\endgroup$
1
  • $\begingroup$ Thank you, @Lukas! That works - gani. $\endgroup$ Dec 9 '19 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.