cancel
Showing results for 
Search instead for 
Did you mean: 

Converting a string to JSON doesn't work

jfpelletier
Contributor

Hello all,

I'm trying to convert a string to a JSON structure, but I'm not succeeding because the format is wrong. The JSON.parse() method says that it's not valid format for a JSON.

Here is the string:

"{language=Tamil, taskId=123},{language=French (Europe), taskId=124},{language=Russian, taskId=125}"

I was hoping that the missing double quotes and that the "=" instead of ":" would be interpreted and fixed, but I'm getting an error:

failure:
"Unable to parse JSON value"
value:
"Please check the format of the JSON value"
reason:
"Please check the format of the JSON value"

I could do some regex to add the missing double quotes and replace the "=" by ":", but is there an easier way or a way that's best recommended?

Thanks in advance to anyone who can help! 🙂

JF

1 ACCEPTED SOLUTION

ptaylor
Employee
Employee

That's the only way since your string is definitely not valid JSON. You need it to look like this:

{"language": "Tamil", "taskId": 123}

View solution in original post

5 REPLIES 5

ptaylor
Employee
Employee

That's the only way since your string is definitely not valid JSON. You need it to look like this:

{"language": "Tamil", "taskId": 123}

Hello again, @ptaylor,

Actually, there is an issue with this...

I took the results below from the Expression Builder. You can see at line 31 that I'm doing all the replacements on all the items in the list (3 elements labelled each "German", "French (Europe)" and "Italian"). At line 32 you can see after the .parse method that my JSON after parsing has only one element now (labelled "German"). So I'm losing everything except the first entry.

31: ([jsonPath($, "$sameLanguage[*].REQUESTEDLANGUAGES").toString().replaceAll('=', '" : "').replaceAll('},{', '"},{"').replaceAll(', ', '", "').replace(/^{/, "{\"").replace(/}$/, "\"}")].toString()):
{
  jsonEncodedString: "{"language" : "German", "taskId" : "43251-1-2"},{"language" : "French (Europe)", "taskId" : "43251-1-3"},{"language" : "Italian", "taskId" : "43251-1-4"}"
}
32: .parse:
[
  {
    language:"German"
    taskId:"43251-1-2"
  }
]

Can you see what I'm doing wrong to have this bad results?

Thanks!

JF

Hello @ptaylor,

I've found a better solution by doing the fixing at the source. The reason why that JSON was badly formatted is because it was a "real" JSON that has been saved as a string in a DB using the .toString() method. I changed to use JSON.stringify() instead, and converting it back to JSON after DB retrieval works much better now, no need to use any string replacements. 🙂

JF

jfpelletier
Contributor

Hello @ptaylor,

Yes I agree, and I managed to make it look like a real JSON (and finally converting it successfully) using a few string replacements, including some regex.

It looks not really clean to me, and I was hoping to fix the format in a simpler way. But it works now, I guess that's the most important... 🙂

Thanks a lot for your reply, have a good rest of your day!

JF