1

I want to run a class where it calls the static resource and input the values in the object. I have a Static Resource name Interest_Tag MIME Type application/json in following JSON format

[
     {
        "NAME": "Healthcare",
        "COGNITIVE_UNIQUE_ID": 10622212216
      },
      {
        "NAME": "Real Estate",
        "UNIQUE_ID": "106ff12216"
      },
      {
        "NAME": "Investigo",
        "UNIQUE_ID": 12516177889
      },
      {
        "NAME": "Verna",
        "UNIQUE_ID": "agagagha1215"
      }
    ]

My Class

public class InterestTags_Insert {

    public static void InterestTags_Insert1(){
        List <DevNiks__Interest_Tag__c> IT = new List<DevNiks__Interest_Tag__c>();
        StaticResource sr= [select id,body from StaticResource Where Name = 'Interest_Tag'];
        String allcontents = sr.body.toString();
        system.debug('allcontents==='+allcontents);

        String str = JSON.serialize(allcontents);
        system.debug('New String = '+str);



         List<StaticResource_InterestTags> StaticResource_InterestTagsResults = (List<StaticResource_InterestTags>)JSON.deserialize(str, List<StaticResource_InterestTags>.class);
        System.debug('StaticResource_InterestTags' +StaticResource_InterestTagsResults);

        for(StaticResource_InterestTags srt :StaticResource_InterestTagsResults ){
            DevNiks__Interest_Tag__c ditc = new DevNiks__Interest_Tag__c();
            ditc.name = srt.NAMEE;
            if(srt.UNIQUE_ID!=null){
            ditc.DevNiks__Unique_ID__c = String.valueof(srt.UNIQUE_ID);
            }
            IT.add(ditc);
            System.debug('IT List ==='+IT);

        }
        Database.insert(IT);

    }


}

My Parser Class

public class StaticResource_InterestTags {

    public String NAMEE;
    public Integer UNIQUE_ID;


    public static List<StaticResource_InterestTags> parse(String json) {
        return (List<StaticResource_InterestTags>) System.JSON.deserialize(json, List<StaticResource_InterestTags>.class);
    }
}

1 Answer 1

3

The variable allcontents is already a JSON string so you don't need to serialize it.

So replace:

String str = JSON.serialize(allcontents);

with:

String str = allcontents;

(When you serialize it, you turn the JSON representing an array into JSON representing single string value, hence the error message.)

4
  • It is throwing another error :- FATAL_ERROR System.StringException: BLOB is not a valid UTF-8 string
    – SFDC_Nik
    Aug 5, 2018 at 11:28
  • 2
    please open a new post for it, as it is a different question.
    – glls
    Aug 5, 2018 at 13:14
  • @SFDC_Nik Sounds like your static resource is not a text string.
    – Keith C
    Aug 5, 2018 at 14:47
  • Thanks @KeithC for the help, it's working now. Static resource was a JSON file which i converted to String and then passed it. Thank you for your help.
    – SFDC_Nik
    Aug 6, 2018 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .