How do i rename a key in JSONObject

i am trying to rename  "displayName" key to"text",

where key "dispalyname" is a part of JSONArray which is nested in JSONObject.



{"description":"Employee","displayName":"Guillaumin, Xavier","email":"","location":"Carretera GDL-ElCastilloKM8.05","title":"Financial Coordinator Global Exports","userId":"HERXGG40"},

{"description":"Former Employee","displayName":"Lugo, Javier","email":"","location":"","title":"Special Projects Manager","userId":"HERJEL40"},

{"description":"Employee","displayName":"Plitnick, Xavier F","email":"","location":"6 Scotch Pine Drive","title":"HZ_1526_Mst Wrap Mech","userId":"HFXFP001"}


Can anyone suggest way to rename ?
JSONArray jsonArray = new JSONArray();
			JSONObject jsonObj = new JSONObject();
			try {
				Collection<User> user = UserManager.getInstance()
				jsonObj.put("jsonText", jsonArray);
			} catch (Exception e) {
			String jsonString = jsonObj.toString();

Open in new window

Who is Participating?
Michel PlungjanConnect With a Mentor IT ExpertCommented:
"If it is not a huge amount of data, ADD the data to a new key"
Michel PlungjanIT ExpertCommented:
Perhaps add the new key with the value and remove the old ?
Michel PlungjanIT ExpertCommented:
In JS:

 jsonText = [ { "description" : "Employee",
        "displayName" : "Guillaumin, Xavier",
        "email" : "",
        "location" : "Carretera GDL-ElCastilloKM8.05",
        "title" : "Financial Coordinator Global Exports",
        "userId" : "HERXGG40"
      { "description" : "Former Employee",
        "displayName" : "Lugo, Javier",
        "email" : "",
        "location" : "",
        "title" : "Special Projects Manager",
        "userId" : "HERJEL40"
      { "description" : "Employee",
        "displayName" : "Plitnick, Xavier F",
        "email" : "",
        "location" : "6 Scotch Pine Drive",
        "title" : "HZ_1526_Mst Wrap Mech",
        "userId" : "HFXFP001"
delete jsonText[0].displayName;

Open in new window

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Amos_MikeAuthor Commented:

"jsonText" is a part of JSON object, it's a key in Json Object. I am sorry,  I cudn't figure out what are you trying to convey?
Michel PlungjanIT ExpertCommented:
trying to convey you probably need to copy the key and delete the old.

Anyway why do you want to rename?
Amos_MikeAuthor Commented:
K got your point..
Thanks , but the change i am trying to do is on server side i.e.  in a servlet.
So i don't think this will help.

I am passing this JSON object to my javascript and there my jquery will read it for an JSONsuggest function.
my JS file for JSON suggest (plugin in jquery) expects the searchdata key to be "text", that's why i need to rename it from displayName to text
Michel PlungjanIT ExpertCommented:
And not easier to change the JS?
Amos_MikeAuthor Commented:
I know that could be the solution, but the thing is i am using that specific key from JS at other places in my application.
Moreover i was curious if there's a workaround, for what i have asked.
Michel PlungjanIT ExpertCommented:
If it is not a huge amount of data, ADD the data to a new key
Amos_MikeAuthor Commented:
i got the solution, i sat and applied my brain's deepest nerves to get it working in JAVA servlet itself.
I am calling this mapBeans fxn, and replacing my fields from JSONArray.
this fxn references the UserMappingBean which has getter and setter for Text field.
It works fine for me.
Thanks for the help.
private Collection<UserMappingBean> mapBeans(Collection<User> users) {
		List<UserMappingBean> beans = new LinkedList<UserMappingBean>();
		for (User user: users) {
			UserMappingBean b = new UserMappingBean();

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.