I got this error “An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. ” when updating a choice field using REST API

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/244176

Domanda

I am working on sharepoint on-premises 2013. and i have a cusotm list which contain 2 columns of type "Choice", as follow:--

enter image description here

Now i am trying to copy one list item and create a new list item, using SharePoint REST API. Currently i am able to populate all the values, but when it comes to the above 2 columns of type choice i received the following error, if the item have the choice unchecked:-

An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.

while if the list item has the checkboxes checked everything will work well. here is part of my API call, where the 2 columns named as "QuickOrder" & "SOFRequired" are being passed to the list api:-

       var QuickOrder= "";
       var SOFRequired="";
    $.ajax({
         url: "*****_api/web/lists/getbytitle('Order Management')/items?$filter=(Id eq "+ orderID+")",
         method: "GET",   
         async: false,
         headers: { "Accept": "application/json; odata=verbose" },
         success: function (data) {
        
            if(data.d.results.length>0){
            
                var items=data.d.results;
                for(var i=0;i<items.length;i++){
                if(items[i].QuickOrder != null && items[i].QuickOrder != "undefined")
                        {
                        
                        QuickOrder = items[i].QuickOrder;
                        
                        }
                        
                        if(items[i].SOFRequired != null && items[i].SOFRequired != "undefined")
                        {
                        
                        SOFRequired = items[i].SOFRequired;
                        
                        }
    //inializet the json object
                var item = {
                "__metadata": { "type": itemType },
                
                "Title": title2 ,
                "QuickOrder":QuickOrder,
                "SOFRequired":SOFRequired
                
                
            };
  //call to create new item  
            $.ajax({
                url: "/*****/_api/web/lists/getbytitle('Order Management')/items",
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(item),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },

Now i think the problem is that sharepoint store the Choice value as an object and not as a direct string value (for example Yes). here is a json response, observed from my F12 tool, for my QuickOrder field, and if the checkbox is not check the value will be = null:-

enter image description here

So can anyone advice how i can fix the error i am getting when passing empty values for my Choice fields?

È stato utile?

Soluzione

The error occurs because the checkbox value is null.

Solution:

Set checkbox value if it is null.

Setps:

Define a empty collection as it is a multiple-values column.

var emptyCheckbox = {"__metadata":{"type":"Collection(Edm.String)"},"results":[]};

Add Else statement to set the checkbox to empty collection if it is null.

if(items[i].QuickOrder != null && items[i].QuickOrder != "undefined"){
    QuickOrder = items[i].QuickOrder;
}else{
    QuickOrder = emptyCheckbox;
}


if(items[i].SOFRequired != null && items[i].SOFRequired != "undefined"){
    SOFRequired = items[i].SOFRequired;
}esle{
    SOFRequired = emptyCheckbox;
}

Summary:

<script src="https://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
var QuickOrder= "";
var SOFRequired="";
var emptyCheckbox = {"__metadata":{"type":"Collection(Edm.String)"},"results":[]};
    $.ajax({
         url: "*****_api/web/lists/getbytitle('Order Management')/items?$filter=(Id eq "+ orderID+")",
         method: "GET",   
         async: false,
         headers: { "Accept": "application/json; odata=verbose" },
         success: function (data) {

            if(data.d.results.length>0){

                var items=data.d.results;
                for(var i=0;i<items.length;i++){
                if(items[i].QuickOrder != null && items[i].QuickOrder != "undefined"){
                    QuickOrder = items[i].QuickOrder;
                }else{
                    QuickOrder = emptyCheckbox;
                }

                if(items[i].SOFRequired != null && items[i].SOFRequired != "undefined"){
                    SOFRequired = items[i].SOFRequired;
                }esle{
                    SOFRequired = emptyCheckbox;
                }                       

                        //inializet the json object
                var item = {
                "__metadata": { "type": itemType },

                "Title": title2 ,
                "QuickOrder":QuickOrder,
                "SOFRequired":SOFRequired


            };
  //call to create new item  
            $.ajax({
                url: "/*****/_api/web/lists/getbytitle('Order Management')/items",
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(item),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },

</script>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top