1

I have sample JsonString and need to mapp the json data with salesforce object. Here my developes code,

public class objectMapp 
{
    //Get id value from the given JSONString
    public Results results;
    public class Results
    {
        public string name;
    }
       public static objectMapp parse(String jsonString) 
       {
        return (objectMapp)JSON.deserialize(jsonString, objectMapp.class);
    }
}

Map Code:
  public class resultClass 
{
    public static void getId()
    {
        String jsonString = '{"count":6176,"next":"https://example.herokuapp.com/objects/recaptures/?page=2","previous":null,"results":[ {"id":"101009","annual_savings":"96613.91","cost":1,},{"id":"101116","annual_savings":"40851.75","cost":1,}}';
        objectMapp output = new objectMapp();
        output = (objectMapp)JSON.deserialize(jsonString,objectMapp.class);
        Member__c objMember = new Member__c();
        objMember.Id_Map__c = output.results.name;
        insert objMember;      
     }
}

enter image description here

Getting the above error while debug the code, whats I missed?

Thanks in Advance.

1 Answer 1

2

First of all, your jsonString is invalid. You need to fix it first if you would like to deserialize JSON to object.

Valid jsonString would look like:

String jsonString = '{"count":6176,"next":"https://example.herokuapp.com/objects/recaptures/?page=2","previous":null,"results":[ {"id":"101009","annual_savings":"96613.91","cost":1},{"id":"101116","annual_savings":"40851.75","cost":1}]}';

Looking into provided JSON your wrapper class should look like:

public class ObjectMap {
    public List<Result> results;
    public class Result {
        public String id;
        public String annual_savings;
        Integer cost;
    }
}

After your comment I refactored wrapper class:

public class MemberWrapper {
    public List<Result> results;

    public class Result {
        public String id;
        public String annual_savings;
        public Decimal cost;
    }

    public List<Member__c> parseMembers() {
        List<Member__c> memberList = new List<Member__c>();
        for(Result result : results) {
            memberList.add(new Member__c(
                    RecordNo__c = result.id,
                    Fee__c = result.cost,
                    Annual_Rate__c = result.annual_savings
            ));
        }
        return memberList;
    }
}

Demonstration how to use class:

String jsonString = '{"count":6176,"next":"https://example.herokuapp.com/objects/recaptures/?page=2","previous":null,"results":[ {"id":"101009","annual_savings":"96613.91","cost":1},{"id":"101116","annual_savings":"40851.75","cost":1}]}';
MemberWrapper memberWrapper = (MemberWrapper)JSON.deserialize(jsonString, MemberWrapper.class);
List<Member__c> members = memberWrapper.parseMembers();
System.debug(LoggingLevel.DEBUG, members);
2
  • Have the above Json data that needs to be insert in to object. ie, In the JSON data, id:001, annual_savings:10001 and cost:1. Need to map this data in to object field like, RecordNo__c==id, Annual_Rate __c== annual_savings and Fee__c==cost.
    – blackFire
    Nov 8, 2021 at 9:42
  • I edited my answer. Nov 8, 2021 at 10:02

You must log in to answer this question.

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