+ Start a Discussion
CHG_pchengCHG_pcheng 

Why JSONParser throws Exception on this JSON string?

Hi All,

 

This is a JSON string returned from our SharePoint. When I use http://jsonlint.com/ to validate it, it says that this is a valid JSON. However, I got this error at tokenCount 35:

 

FATAL_ERROR|System.JSONException: Unexpected character ('N' (code 78)): was expecting comma to separate OBJECT entries at input location [1,812]

 

if I'm trying to parse it in Developer Console using the following code:

 

String JSONContent =
'{"d":{"query":{"__metadata":{"type":"Microsoft.Office.Server.Search.REST.SearchResult"},"ElapsedTime":578,"PrimaryQueryResult":{"__metadata":{"type":"Microsoft.Office.Server.Search.REST.QueryResult"},"CustomResults":null,"QueryId":"012759af-2eed-4bf8-a46b-8bd4e72e49df","QueryRuleId":"00000000-0000-0000-0000-000000000000","RefinementResults":null,"RelevantResults":{"__metadata":{"type":"Microsoft.Office.Server.Search.REST.RelevantResults"},"GroupTemplateId":null,"ItemTemplateId":null,"Properties":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"GenerationId","Value":"9223372036854775806","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExecutionTimeMs","Value":"31","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"QueryModification","Value":"TimeEntered:\"No\" AND Audited:\"No\" SPContentType:\"Work Record\" FileType:pdf","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"StartRecord","Value":"0","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"IsLastBlockInSubstrate","Value":"true","ValueType":"Edm.Boolean"},{"__metadata":{"type":"SP.KeyValue"},"Key":"IsFirstBlockInSubstrate","Value":"false","ValueType":"Edm.Boolean"},{"__metadata":{"type":"SP.KeyValue"},"Key":"IsFirstPinnedResultBlock","Value":"false","ValueType":"Edm.Boolean"},{"__metadata":{"type":"SP.KeyValue"},"Key":"IsLastPinnedResultBlock","Value":"false","ValueType":"Edm.Boolean"},{"__metadata":{"type":"SP.KeyValue"},"Key":"IsFirstRankedResultBlock","Value":"true","ValueType":"Edm.Boolean"},{"__metadata":{"type":"SP.KeyValue"},"Key":"IsLastRankedResultBlock","Value":"true","ValueType":"Edm.Boolean"}]},"ResultTitle":null,"ResultTitleUrl":null,"RowCount":6,"Table":{"__metadata":{"type":"SP.SimpleDataTable"},"Rows":{"results":[{"__metadata":{"type":"SP.SimpleDataRow"},"Cells":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"Rank","Value":"6.91106796264648","ValueType":"Edm.Double"},{"__metadata":{"type":"SP.KeyValue"},"Key":"DocId","Value":"2277","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExcludeFromSummary","Value":"WK457YX4XDMS-3-3","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentName","Value":"ASG-855213","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ProviderName","Value":"FOX Provider1","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Specialty","Value":"Anesthesiology","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodStartDate","Value":"2013-02-11T07:00:00.0000000Z","ValueType":"Edm.DateTime"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentStartDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"TimeEntered","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Audited","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PartitionId","Value":"0c37852b-34d0-418e-91c6-2ac25af4be5b","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"UrlZone","Value":"3","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AAMEnabledManagedProperties","Value":"AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","ValueType":"Edm.String"}]}},{"__metadata":{"type":"SP.SimpleDataRow"},"Cells":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"Rank","Value":"6.69746589660645","ValueType":"Edm.Double"},{"__metadata":{"type":"SP.KeyValue"},"Key":"DocId","Value":"2365","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExcludeFromSummary","Value":"WK457YX4XDMS-3-9","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentName","Value":"ASG-861332","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ProviderName","Value":"FOX Provider1","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Specialty","Value":"Anesthesiology","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodStartDate","Value":"2013-02-11T07:00:00.0000000Z","ValueType":"Edm.DateTime"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentStartDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"TimeEntered","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Audited","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PartitionId","Value":"0c37852b-34d0-418e-91c6-2ac25af4be5b","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"UrlZone","Value":"3","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AAMEnabledManagedProperties","Value":"AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","ValueType":"Edm.String"}]}},{"__metadata":{"type":"SP.SimpleDataRow"},"Cells":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"Rank","Value":"6.69746589660645","ValueType":"Edm.Double"},{"__metadata":{"type":"SP.KeyValue"},"Key":"DocId","Value":"2366","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExcludeFromSummary","Value":"WK457YX4XDMS-3-10","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentName","Value":"ASG-861333","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ProviderName","Value":"FOX Provider2","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Specialty","Value":"Cardiology","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodStartDate","Value":"2013-02-11T07:00:00.0000000Z","ValueType":"Edm.DateTime"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentStartDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"TimeEntered","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Audited","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PartitionId","Value":"0c37852b-34d0-418e-91c6-2ac25af4be5b","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"UrlZone","Value":"3","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AAMEnabledManagedProperties","Value":"AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","ValueType":"Edm.String"}]}},{"__metadata":{"type":"SP.SimpleDataRow"},"Cells":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"Rank","Value":"6.36986923217773","ValueType":"Edm.Double"},{"__metadata":{"type":"SP.KeyValue"},"Key":"DocId","Value":"2279","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExcludeFromSummary","Value":"WK457YX4XDMS-3-4","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentName","Value":"ASG-855218","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ProviderName","Value":"FOX Provider1","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Specialty","Value":"Anesthesiology","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodStartDate","Value":"2013-02-25T07:00:00.0000000Z","ValueType":"Edm.DateTime"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentStartDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"TimeEntered","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Audited","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PartitionId","Value":"0c37852b-34d0-418e-91c6-2ac25af4be5b","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"UrlZone","Value":"3","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AAMEnabledManagedProperties","Value":"AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","ValueType":"Edm.String"}]}},{"__metadata":{"type":"SP.SimpleDataRow"},"Cells":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"Rank","Value":"6.36986923217773","ValueType":"Edm.Double"},{"__metadata":{"type":"SP.KeyValue"},"Key":"DocId","Value":"2280","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExcludeFromSummary","Value":"WK457YX4XDMS-3-5","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentName","Value":"ASG-855215","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ProviderName","Value":"FOX Provider1","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Specialty","Value":"Anesthesiology","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodStartDate","Value":"2013-03-11T06:00:00.0000000Z","ValueType":"Edm.DateTime"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentStartDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"TimeEntered","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Audited","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PartitionId","Value":"0c37852b-34d0-418e-91c6-2ac25af4be5b","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"UrlZone","Value":"3","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AAMEnabledManagedProperties","Value":"AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","ValueType":"Edm.String"}]}},{"__metadata":{"type":"SP.SimpleDataRow"},"Cells":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"Rank","Value":"6.36986923217773","ValueType":"Edm.Double"},{"__metadata":{"type":"SP.KeyValue"},"Key":"DocId","Value":"2281","ValueType":"Edm.Int64"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ExcludeFromSummary","Value":"WK457YX4XDMS-3-6","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentName","Value":"ASG-855214","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"ProviderName","Value":"FOX Provider2","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Specialty","Value":"Cardiology","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodStartDate","Value":"2013-02-11T07:00:00.0000000Z","ValueType":"Edm.DateTime"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PayPeriodEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentStartDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AssignmentEndDate","Value":null,"ValueType":"Null"},{"__metadata":{"type":"SP.KeyValue"},"Key":"TimeEntered","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"Audited","Value":"No","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"PartitionId","Value":"0c37852b-34d0-418e-91c6-2ac25af4be5b","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"UrlZone","Value":"3","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"AAMEnabledManagedProperties","Value":"AttachmentURI;deeplinks;DefaultEncodingURL;ExternalMediaURL;HierarchyUrl;OrgParentUrls;OrgUrls;OriginalPath;ParentLink;Path;PictureThumbnailURL;PictureURL;PublishingImage;recommendedfor;ServerRedirectedEmbedURL;ServerRedirectedPreviewURL;ServerRedirectedURL;SiteLogo;SitePath;SPSiteURL;UserEncodingURL","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"RenderTemplateId","Value":"~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default.js","ValueType":"Edm.String"}]}}]}},"TotalRows":6,"TotalRowsIncludingDuplicates":8},"SpecialTermResults":null},"Properties":{"results":[{"__metadata":{"type":"SP.KeyValue"},"Key":"RowLimit","Value":"10","ValueType":"Edm.Int32"},{"__metadata":{"type":"SP.KeyValue"},"Key":"SourceId","Value":"0bbbac76-ec01-4bb9-b789-05d3a1b6583e","ValueType":"Edm.Guid"},{"__metadata":{"type":"SP.KeyValue"},"Key":"EnableInterleaving","Value":"true","ValueType":"Edm.Boolean"},{"__metadata":{"type":"SP.KeyValue"},"Key":"piPageImpression","Value":"3334_3114_1033","ValueType":"Edm.String"},{"__metadata":{"type":"SP.KeyValue"},"Key":"SerializedQuery","Value":"<Query Culture=\"en-US\" EnableStemming=\"True\" EnablePhonetic=\"False\" EnableNicknames=\"False\" IgnoreAllNoiseQuery=\"True\" SummaryLength=\"180\" MaxSnippetLength=\"180\" DesiredSnippetLength=\"90\" KeywordInclusion=\"0\" QueryText=\"TimeEntered:&quot;No&quot; AND Audited:&quot;No&quot; SPContentType:&quot;Work Record&quot;\" QueryTemplate=\"\" TrimDuplicates=\"True\" Site=\"45a6d344-c398-44cb-a686-f91abcd8232d\" Web=\"09ad0561-9701-4fd5-89c1-2d3c34c7be86\" KeywordType=\"True\" HiddenConstraints=\"\" />","ValueType":"Edm.String"}]},"SecondaryQueryResults":null,"SpellingSuggestion":"","TriggeredRules":{"results":[]}}}}';
JSONParser parser = JSON.createParser(JSONContent);
Integer tokenCount =0;
while (parser.nextToken() != null) {
tokenCount++;
System.debug('tokenCount='+tokenCount);
System.debug('getCurrentToken()='+parser.getCurrentToken());
System.debug('getText()='+parser.getText());
System.debug('getCurrentName()='+parser.getCurrentName());
System.debug('nextValue()='+parser.nextValue());
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
System.debug('Got a field name!');
}
}

 

 

sfdcfoxsfdcfox
Quotes have to be escaped in JSON, and backslashes have to be escaped in Apex Code. It's failing on \", because \" doesn't generate a valid string character. I copied your code into notepad, replaced \ with \\, and the string parsed successfully with no other modifications. Note that you should NOT attempt to escape backslashes from an actual string source, such as a HttpResponse object. You only need to do this when trying to encode literal backslashes in Apex Code strings.
CHG_pchengCHG_pcheng

Thank you so very much sfdcfox.

Yes the error is gone when I did what you suggested. Trying to pull only the data that I need. Hopefully things will go well from here.