1

I am working on a web application based on struts+java+hibernate framework. The underlying database is MySQL 5.

The user activities are recorded in the DB and later on shown to other users. When a user does some activity, the information pertaining to the activity is also stored along with the activity. There is now an implementation change. Earlier this activity information was stored as plain text but now it is stored as json for ease of usage(considering the fact that this information can have many values as null).

The information stored is later on used for displaying the activity on the web page.

For all the new activities, (information)data gets JSON encoded in the java code and encoded JSON string is stored in DB. The problem is with data corresponding to the old activities.

Is there any way to JSON encode the data stored in DB?. Or Is there any way in jquery/javascript to encode the string in JSON format and then get the Json object constructed back properly?

The general format of the data to be stored in DB(after migration) is:

{"key1":"value1", "key2":"value2","key3":"value3",...}

and data already in DB is in format:

value1,value2,value3,... 

These values (namely value1,value2, value3,...) can contain any special characters.

3
  • JSON is an acronym for JavaScript Object Notation, so your asking if there is a JS way to encode/decode a JavaScript Object from and to a string? The answer is yes: JSON.parse, JSON.stringify. The format in your DB looks pretty JSON-y to me already... Commented Jul 1, 2013 at 12:58
  • The problem with JSON.stringify is that it also encodes the outer quotes enclosing the keys and values resulting into {\"key1\":\"value1\", \"key2\":\"value2\",\"key3\":\"value3\",...} which rules out the usage of JSON.stringify. Also I can't parse the string as it is because the values(value1, value2,...) can contain special characters themselves like ",#,$,%,<,>,(,),@, etc. Commented Jul 1, 2013 at 13:08
  • 1
    Of course JSON.stringify will escape quotes, if it didn't they'd be string delimiters. The escaping `` isn't being escaped, so it's not part of the string itself, it merely escapes the quote. Unless you're stringifying the the object twice. Special chars shouldn't be an issue, if they're part of the string, they're just a char constant, nothing more, nothing less Commented Jul 1, 2013 at 13:15

3 Answers 3

1

Maybe you wanna take a look at JSON.parse and JSON.stringify methods. http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

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

3 Comments

I have tried these but JSON.stringify also escapes the outer quotes enclosing the keys and values resulting into {\"key1\":\"value1\", \"key2\":\"value2\",\"key3\":\"value3\",...} which is not the desired result.
Hm... you can always break your head and create a parser yourself, that suits your needs.
hmmm... Seems like that is the only option left. Thanks for the suggestion :)
0

In jQuery you can use the following code to encode the json:

var data  = {"key1":"value1", "key2":"value2","key3":"value3",...};
$.each(data,function(index,value)){
    alert(index+" | "+value);
});

use the $.each function of jQUery.

2 Comments

Is there any better way to do this in place of manually iterating over the string and doing the encoding?
@NishaGoyal ji this is the way to encode the json. as json is the OBJECT notation of javascript.
0

Solved it as following:

say the above json string is in a variable named info then first replace all the outer double quotes to key and values with single quotes like this

info = info.replace(/{\"/g,"{'");
info = info.replace(/\"}/g,"'}");
info = info.replace(/\",\"/g,"','");
info = info.replace(/\":\"/g,"':'");

Now do the JSON stringification:

info = JSON.stringify(info);

Now restore back the outer double quotes and remove the extra quotes added on JSON stringification:

info = info.replace(/\"{'/g,"{\"");
info = info.replace(/'}\"/g,"\"}");
info = info.replace(/','/g,"\",\"");
info = info.replace(/':'/g,"\":\""); 

Now do JSON parsing:

info = JSON.parse(info);

The result is a JSON object.

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.