Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : 3KUCA3xuyNDq6Kl1pidCZm
Finance | Project Operations, Human Resources, ...
Answered

Error in Parsing the JSON in Batch Job

Like (1) ShareShare
ReportReport
Posted on 6 May 2024 06:40:59 by 814
Hi Users,
I am trying to parse the JSON using Below link:
 
I copied the Newtonsoft.Json.dll into client and server bin folder, also added reference in AOT.
 
Parsing of JSON works fine in simple job running, but not working in Batch Job.
 
Getting below error:
 
on the line number 50 in the code below.
 
​private anytype traversePath(str                               path,                             Newtonsoft.Json.Linq.JContainer   obj = jObject){    List                            pathElements;    ListEnumerator                  le;    Newtonsoft.Json.Linq.JValue     value;    Newtonsoft.Json.Linq.JToken     token;    Newtonsoft.Json.Linq.JTokenType thisType,                                    nestedType;    Newtonsoft.Json.Linq.JObject    newObject;    Newtonsoft.Json.Linq.JArray     newArray;    str                             current,                                    thisTypeString,                                    nestedTypeString;     #define.JObject(/Newtonsoft.Json.Linq.JObject/)    #define.JArray (/Newtonsoft.Json.Linq.JArray/)     ;     pathElements = strSplit(path, @/.////);     le = pathElements.getEnumerator();     if (le.moveNext())    {        current = le.current();         thisType = obj.GetType();        thisTypeString = thisType.ToString();         switch (thisTypeString)        {            case #JObject:                token = obj.get_Item(current);                break;            case #JArray:                token = obj.get_Item(str2int(current) - 1);                break;            default:                return null;        }         if (token)        {            nestedType = token.GetType();            nestedTypeString = nestedType.ToString();             if (nestedTypeString != #JObject && nestedTypeString != #JArray)            {                switch (thisTypeString)                {                    case #JArray:                        return obj.get_Item(str2int(current) - 1);                    case #JObject:                        return obj.get_Item(current);                    default:                        return null;                }            }                         switch (nestedTypeString)            {                case #JObject:                    newObject = Newtonsoft.Json.Linq.JObject::FromObject(token);                    return this.traversePath(strDel(path, 1, strLen(current) + 1), newObject);                case #JArray:                    newArray = Newtonsoft.Json.Linq.JArray::FromObject(token);                    return this.traversePath(strDel(path, 1, strLen(current) + 1), newArray);                default:                    return null;            }        }        else        {            return null;        }    }    else    {        return null;    }}​
 
 
If you know anything about the issue, Plese reply.
 
Thank you,
Raj Borad
 
  • Raj Borad Profile Picture
    814 on 07 May 2024 at 05:38:46
    Error in Parsing the JSON in Batch Job
    Hi Martin, 
     
    I tried since yesterday to tick the Does this answer your question?
    but it is not ticked, and page is gone in reload.
     
    Please try to mark your reply as a verified answer, if you can...
     
    Thank you,
    Raj Borad
  • Martin Dráb Profile Picture
    232,850 Most Valuable Professional on 07 May 2024 at 05:24:14
    Error in Parsing the JSON in Batch Job
    Did it solve your problem? If so, please mark the helpful reply (or replies) as the answer (by Does this answer your question?). If it doesn't work for you (there is a known problem with the site), I (as a moderator) can try it on your behalf.
  • Raj Borad Profile Picture
    814 on 06 May 2024 at 09:09:09
    Error in Parsing the JSON in Batch Job
    Martin,
    Thank you for your quick response.
     
    Regards,
    Raj Borad
  • Verified answer
    Martin Dráb Profile Picture
    232,850 Most Valuable Professional on 06 May 2024 at 08:12:58
    Error in Parsing the JSON in Batch Job
    OK, I see. GetType() returns System.Type, but you're trying to assign it to a variable of type Newtonsoft.Json.Linq.JTokenType, which isn't possible. I think you mean Type property, which you can access by get_Type() method. GetType() has a similar name, but it's something completely different. It's a method inherited from System.Object that gives you an instance of System.Type describing the type of the object in the variable.
     
     
  • Raj Borad Profile Picture
    814 on 06 May 2024 at 07:46:27
    Error in Parsing the JSON in Batch Job
    Hi Martin,

    I am using AX 2012.

    And for obj variable:
     jObject = Newtonsoft.Json.Linq.JObject::Parse(_json);   // _json is a string.
     
    and stored it in obj = jObject;
     
     While debugging the code, I get the error in that line so...
  • Martin Dráb Profile Picture
    232,850 Most Valuable Professional on 06 May 2024 at 07:20:23
    Error in Parsing the JSON in Batch Job
    What type of object do you have in your obj variable?
     
    Is it about AX 2009 or another version?
     
    How did you determine that the highlighted line is to blame? It sounds strange to me that obj.GetType() would call Convert.ChangeType() (but I can't rule it out).
  • Raj Borad Profile Picture
    814 on 06 May 2024 at 06:54:29
    Error in Parsing the JSON in Batch Job
    HI here is the code.
     
    private anytype traversePath(str                               path,
                                 Newtonsoft.Json.Linq.JContainer   obj = jObject)
    {
        List                            pathElements;
        ListEnumerator                  le;
        Newtonsoft.Json.Linq.JValue     value;
        Newtonsoft.Json.Linq.JToken     token;
        Newtonsoft.Json.Linq.JTokenType thisType,
                                        nestedType;
        Newtonsoft.Json.Linq.JObject    newObject;
        Newtonsoft.Json.Linq.JArray     newArray;
        str                             current,
                                        thisTypeString,
                                        nestedTypeString;
     
        #define.JObject("Newtonsoft.Json.Linq.JObject")
        #define.JArray ("Newtonsoft.Json.Linq.JArray")
     
        ;
     
        pathElements = strSplit(path, @".\/");
     
        le = pathElements.getEnumerator();
     
        if (le.moveNext())
        {
            current = le.current();
     
            thisType = obj.GetType();
            thisTypeString = thisType.ToString();
     
            switch (thisTypeString)
            {
                case #JObject:
                    token = obj.get_Item(current);
                    break;
                case #JArray:
                    token = obj.get_Item(str2int(current) - 1);
                    break;
                default:
                    return null;
            }
     
            if (token)
            {
                nestedType = token.GetType();
                nestedTypeString = nestedType.ToString();
     
                if (nestedTypeString != #JObject && nestedTypeString != #JArray)
                {
                    switch (thisTypeString)
                    {
                        case #JArray:
                            return obj.get_Item(str2int(current) - 1);
                        case #JObject:
                            return obj.get_Item(current);
                        default:
                            return null;
                    }
                }
                 
                switch (nestedTypeString)
                {
                    case #JObject:
                        newObject = Newtonsoft.Json.Linq.JObject::FromObject(token);
                        return this.traversePath(strDel(path, 1, strLen(current) + 1), newObject);
                    case #JArray:
                        newArray = Newtonsoft.Json.Linq.JArray::FromObject(token);
                        return this.traversePath(strDel(path, 1, strLen(current) + 1), newArray);
                    default:
                        return null;
                }
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,998 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,850 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans
Loading started