+ Start a Discussion
Poorna DeveloperPoorna Developer 

JSON Deserialize and update in to custom field

Helo Everyone,

 I have created two custom field named Amount__c and Connection_Id under the Object 'Order'.

And I have perform callout and get response from the endurl in the form of JSON Data.
Here, the sample JSON file:
 {"data":[{"id":"2911647985786786830357753","connection_id":"291165354723552979434","name":"Simple account 1 MasterCard","nature":"card","balance":2019.4,"currency_code":"EUR", "extra":{ "iban":"DE11100110012612442222",},"created_at":"2020-08-24T05:58:45Z","updated_at":"2021-04-02T07:34:19Z"}

I need to deserialize this file and separate the Balance and connection_id from the json file and store this value in the Amount_c and Connection_Id__c in the 'Order' object.

Is there any suggestion??
Thanks in advance.
Maharajan CMaharajan C
Hi Poorna,

I have assuemed the below is your correct JSON:
 
{
 	"data": [{
 		"id": "2911647985786786830357753",
 		"connection_id": "291165354723552979434",
 		"name": "Simple account 1 MasterCard",
 		"nature": "card",
 		"balance": 2019.4,
 		"currency_code": "EUR",
 		"extra": {
 			"iban": "DE11100110012612442222"
 		},
 		"created_at": "2020-08-24T05:58:45Z",
 		"updated_at": "2021-04-02T07:34:19Z"
 	}]
 }

If the above is your JSON then add the below class seperately:
 
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public List<Data> data;

	public class Extra {
		public String iban;
	}

	public class Data {
		public String id;
		public String connection_id;
		public String name;
		public String nature;
		public Double balance;
		public String currency_code;
		public Extra extra;
		public String created_at;
		public String updated_at;
	}

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
}


Then in your actual class add the below code:
 
JSON2Apex myClass = new JSON2Apex();
JSON2Apex myClass = JSON2Apex.parse(response.getBody());

List<JSON2Apex.Data> d = myClass.data;

Order o = new Order();
o.Connection_Id__c  = d[0].connection_id;
o.Amount__c  = d[0].balance;

Thanks,
Maharajan.C

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Poorna,

Greetings!

Please use the Wrapper class for creating a JSON.
Wrapper class will help you to create understandable JSON and you will manage it easily.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi