1

I've got a bit of a problem. I'm currently working on converting an old system to a newer version. In the old version, data requests were managed by Java applets, which had no problems with the "-char in the data. Now, though, I'm fetching the data from a database and converting it to a JSON-string using XSLT, and then - with the prototype-function .evalJSON() - making the string into an object. The XSL then structure the data like this (example) :

{rowsets: [ { rows: [ { "ID":"xxx","OtherProperty":"yyy" } ] } ] }

Which in it self is OK. Now,when there's some data in the database containing "-characters, the evalJSON() fails, because it destroys the usually well-formatted JSON string, like this:

{rowsets: [ { rows: [ { "ID":"xxx","OtherProperty":"yyy "more" zzz" } ] } ] }

Now, what i want to do, is escape the 'unwanted' "-chars somehow - without having to make some kind of Stored Procedure to du it server-side for me. I've tried to wrap my head around a RegEx, but I'm not very experienced in that area, and therefore I'm having a really hard time figuring it out.

If it's any help, the character sequences that are sure to be legal are: [":"] and [","] and the sequences that are likely to appear, and should be escaped, are: [\s"], ["\s], [",], [".] (\s indicates a whitespace)

All kinds of help is appreciated, even if it's some SQL that makes it all a lot easier :)

Thanks in advance!

3
  • Your conversion needs to generate valid JSON! Commented Jul 15, 2010 at 15:12
  • It is very valid JSON, as long as there's no "-characters in the data set fetched from the database. Those characters is what i need to escape using some regular expression or something, before using the evalJSON() function. Commented Jul 16, 2010 at 6:47
  • "'m fetching the data from a database and converting it to a JSON-string using XSLT" - huh? Please post that code. This is what you need to fix to do the escaping. Commented Oct 6, 2023 at 14:48

2 Answers 2

1

If you're in XSLT land then you're reinventing the wheel. Google up "badgerfish" and see here for a fairly solid implementation. You may of course have other problems getting in the way, but first things first.

Sign up to request clarification or add additional context in comments.

2 Comments

Looking at the link you've given me, I can see that it's going to output the data just like the XSLT I'm using at the moment (ex. from site: { "alice": { "bob": [ { "$": "charlie" }, { "$": "david" } ] } }) - using "" to seperate properties and data is OK as long as there's no "-characters in the data - which in my case there is.
@freaktm: No, the xslt includes escape replacing content i.e. " -> \". Try it.
0

I ended up taking a shortcut, using a string-replace template in my XSL to replace the "-characters with " before returning the JSON to my javascript function, thus not needing to escape anything client-side.

(Used this: Link)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.