I am trying to extract the 'key' from a json an insert each key to a list (array).

Map<String,Object> obj = (Map<String,Object>) JSON.deserializeUntyped(reqestObj);
List<String> searchFields = new List<String>(); 
System.debug('POINT 1==='+searchFields.get(0)); 
System.debug('POINT 2==='+searchFields.get(1));
System.debug('POINT 3==='+searchFields.get(2));

Suppose below is the reqestObj json


When i debug searchFields gets the following:

searchFields===({City, OmniId, State})

Again when i tried to debug each individual element in the searchFields find an extra brace.

POINT 1==={City

POINT 2=== OmniId

POINT 3=== State}

Please help me to get rid of that. I believe there is a simple way of extracting the required value without using extra valriabe or using replaceAll() function etc.

  • Once you have the keyset, which gives you list of keys, any reason you are joining to a string and then split?
    – metasync
    Apr 3, 2018 at 15:00
  • nope. I just want the keys.
    – saugandh k
    Apr 3, 2018 at 15:03

2 Answers 2


You don't need to do any of that string conversion mechanic to convert a Set<String> to a List<String>. The List class has a constructor that creates the List from a Set:

public List<T>(Set<T> setToCopy)

Just do

List<String> searchFields = new List<String>(obj.keySet()); 

While David's answer give you correct way to do it, I want to highlight why you get { in the output.

When you do String.valueOf(obj.keyset().clone()) it creates a string which looks like {<contents of list/set>} and then when you split, first entry includes { and last entry includes }.

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.