Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:44:48+00:00 2026-05-26T05:44:48+00:00

I’m getting ‘invalid JSON primitive’ when i try to deserialize a JSON blob generated

  • 0

I’m getting ‘invalid JSON primitive’ when i try to deserialize a JSON blob generated by log4javascript using the ajax appender with the json layout.

I’ve written a web service method to recieve the JSON sent by log4javascript but the JSON is declared invalid and an exception thrown when .NET tries to deserialize it and I can’t understand why.

The JS setup looks like this :

var requestURL = (
    $.url.build({
       "path": "ClientSideLoggingSupport.asmx/Log"
    })
);
    var requestURLEncoded = $.url.encode(requestURL)
    var log4js = log4javascript.getLogger();
    var ajaxAppender = new log4javascript.AjaxAppender(requestURLEncoded);
    ajaxAppender.setLayout(new log4javascript.JsonLayout(false, false));
    log4js.addAppender(ajaxAppender);
    log4js.info("Message 1");

The ‘log’ method looks like this :

[WebMethod(Description = "Accepts a request to log client side information")]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public void Log()
{
    try
    {
        System.Collections.Specialized.NameValueCollection nvcss = this.Context.Request.Form;
        var jss = new JavaScriptSerializer();
        string s = nvcss["data"].ToString();
        Dictionary<string, string> x = jss.Deserialize<Dictionary<string, string>>(nvcss["data"].ToString());

        var dict = jss.Deserialize<Dictionary<string, string>>(nvcss["data"]);
        _NLogLogger.Info("The logging web service was invoked");
    }
    catch (Exception ex)
    {
        _NLogLogger.Error("Some sort of problem occurred1");
        _NLogLogger.Error(String.Format("Ex.Message = {0}", ex.Message));
        _NLogLogger.Error(String.Format("Ex.Source = {0}", ex.Source));
        _NLogLogger.Error(String.Format("Ex.StackTrace = {0}", ex.StackTrace));
        _NLogLogger.Error(String.Format("Ex.InnerException = {0}", ex.InnerException));
        throw;
    }
}

The value of s in the above method looks like this just before the exception is thrown : :

"[{\"logger\":\"[anonymous]\",\"timestamp\":1318394483187,\"level\":\"INFO\",\"url\":\"http://localhost:19019/Logon.aspx\",\"message\":[\"Message 1\"],}]"

That looks to me like a plausible string to feed the deserializer but clearly it’s not ! Anyone know what I (or log4javascript) is doing wrong ?


OK I’ve realised something about my code which is wrong but fixing that doesn’t actually fix the problem.

The JSON represents an array of objects and so the object receiving the results of the deserialization needs to a list of dictionaries. So I have amended the deserialization code as follows :

//Alter instantion of JavaScriptSerializer                
JavaScriptSerializer jsss = new JavaScriptSerializer();
//Request Deserialization presuming a list of dictionaries
List<Dictionary<string, string>> xx = jsss.Deserialize<List<Dictionary<string, string>>>(nvcss["data"]);

So this is, I believe better, but still throws the same exception.

For what it’s worth I now have the following line of code within the method and it executes fine so it looks increasingly as if it’s the JSON produced by log4javascript which needs adjusting in some way

List<Dictionary<string, string>> o = jsss.Deserialize<List<Dictionary<string, string>>>("[{'a':1,'b':2},{'a':10,'b':20}]");

Any suggestions welcome.


More progress (of a sort) after noticing that the ‘message’ attribute of the JSON was itself an array I temporarily changed ‘message’ so that it’s a scaler. In this way the JSON looks like this:

List<Dictionary<string, string>> ooo = jsss.Deserialize<List<Dictionary<string, string>>>("[{\"logger\":\"[anonymous]\",\"timestamp\":1318394483187,\"level\":\"INFO\",\"url\":\"http://localhost:19019/Logon.aspx\",\"message\":\"Message 1\"}]");

… and this is then able to execute so it seems like the problem is that the JSON has an embedded array and so a different target object will need to be specified in order to deserialize the JSON successfully.


Final edit ! Thanks to Tim spotting the flaw in the JSON I’ve now got a working version. I’ve pretended the JSON is doing the right thing and then just dumped out the message payload in a fairly unelegant fashion but it may be useful to someone in future.

List<Dictionary<string, object>> lstMsg = jsss.Deserialize<List<Dictionary<string, object>>>("[{\"logger\":\"[anonymous]\",\"timestamp\":1318394483187,\"level\":\"INFO\",\"url\":\"http://localhost:19019/Logon.aspx\",\"message\":[\"Message 1\"]}]");
string sOut = "";
foreach (Dictionary<string, object> m in lstMsg)
{
    sOut = sOut + m["timestamp"];
    foreach(string sMessage in (ArrayList)m["message"])
    {
    sOut = sOut + "|" + sMessage;
    }
}
_NLogLogger.Info("sOut:" + sOut);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:44:48+00:00Added an answer on May 26, 2026 at 5:44 am

    The problem is that the JSON log4javascript is producing has a trailing comma here:

    \"message\":[\"Message 1\"],
    

    … which is invalid, and somewhat embarrassing for me as log4javascript’s developer.

    I’ll fix this and release a new version as soon as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.