function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
S DS D 

REST API for Inserting/Updating 2-3 records

Can anyone please help me with this:---

I want to  only insert 3 records using POST Method. and as its not a bulk request so i am not preferring  BULK API.

also as JSON does support the storage of data as array, so believe the below format is good to go for. 

But am not able to understand how would this be processed further in Apex Class???  How can i pass it as a  signature to the class. I tried to do and got the below error.

format of JSON :

{"merch" : [
{"name" : "Eraser1", "description" : "desc", "price" = 2, "Inventory" : 1000},
{"name" : "Eraser2", "description" : "desc", "price" = 20, "Inventory" : 9000},
{"name" : "Eraser3", "description" : "desc", "price" = 28, "Inventory" : 6000}
]}

Error:
"message" : "Unexpected parameter encountered during deserialization: merch at [line:1, column:12]",
  "errorCode" : "JSON_PARSER_ERROR"
} ]

I am not sure what does this error mean??
James LoghryJames Loghry
It means your JSON is formatted incorrectly :)

One tip is to use this awesome tool, and see how it generates the Apex model for you, and then see if that lines up with how you're trying to cast the JSON string.  https://json2apex.herokuapp.com/

I
 ran your JSON through the tool, and it told me that you have to change "=" to ":".  After doing that, here's what the generated apex looked like:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public List<Merch> merch;

	public class Merch {
		public String name;
		public String description;
		public Integer price;
		public Integer Inventory;
	}

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
	
	static testMethod void testParse() {
		String json = '{\"merch\" : ['+
		'{\"name\" : \"Eraser1\", \"description\" : \"desc\", \"price\" : 2, \"Inventory\" : 1000},'+
		'{\"name\" : \"Eraser2\", \"description\" : \"desc\", \"price\" : 20, \"Inventory\" : 9000},'+
		'{\"name\" : \"Eraser3\", \"description\" : \"desc\", \"price\" : 28, \"Inventory\" : 6000}'+
		']}';
		JSON2Apex obj = parse(json);
		System.assert(obj != null);
	}
}


S DS D
Thank you !! aww i should have checked it.

I am just curious to know, post this generation of Class...Do we have to us this somewhere in Saleforce. ???? as I am still getting this error,with the updated JSON request. Could you pl guide.


Update JSON Input given:

{"merch" : [
{"name" : "Eraser1", "description" : "desc", "price" : 2, "Inventory" : 1000},
{"name" : "Eraser2", "description" : "desc", "price" : 20, "Inventory" : 9000},
{"name" : "Eraser3", "description" : "desc", "price" : 28, "Inventory" : 6000}
]}

my POST Method looks like this:

@HttpPost
    global static String createMerchandise(String jn)
    {
        system.debug(jn); // to verify the how the JSON input looks like
        return 'done';
   }

Got Same error: 

Raw Response
HTTP/1.1 400 Bad Request
Date: Wed, 30 Jul 2014 14:11:57 GMT
Set-Cookie: BrowserId=6tJ-U4wjSdmAaWcpiNJdGw;Path=/;Domain=.salesforce.com;Expires=Sun, 28-Sep-2014 14:11:57 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked


[ {
  "message" : "Unexpected parameter encountered during deserialization: merch at [line:1, column:13]",
  "errorCode" : "JSON_PARSER_ERROR"
} ]





S DS D
I worked on making a small change onto my  REST Apex Class, and it showing me a same issue in a different wording i guess.

Error now is : 
[ {
  "message" : "Expected JSON object to deserialize apex parameter from at [line:2, column:2]",
  "errorCode" : "JSON_PARSER_ERROR"
} ]

to fix this issue, i have modified my class to : But it dint help :

@HttpPost
    global static String createMerchandise(String merch)
    {
     
     List<Merchandise__c> tt =   (List<Merchandise__c>)System.JSON.deserialize(merch, List<Merchandise__c>.class);
        system.debug(tt);
   return 'done'
}
S DS D
This is fixed.

Logic is you need to get the JSON data in a array parameter of Apex Class, and start consuming it.