This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to access the array[] values in C# scripted rule

Hi Team,

I have the below JSON Request, trying to access the values inside the C# scripted rule. 

{
    "stocks": [
        {
            "color""Blue",
            "name""YTDD1",
            "itamid""8740-YTD"
        },
        {
     
             "color""Red",
            "name""YTDD2",
            "itamid""8741-YTD"
        }
        
    ]
}

Inside loop I would like to get the color value from request and set it to JSON response. the JSON structure is already imported, I am only trying to change value based on some condition. I don't find any documentation for how to work with JSON/XML payload array of items.

I have tried something like this but its throwing Null pointer exception.
string color =sv.Request.JSONRequest.stocks.i[0].color;



I am using 5.4.1 version
Request your Support 

   
  • 0

    Hi Community,

    Can anyone please update the answer for the above question?

    Thanks in advance.

       

  • 0   in reply to 

    I tried the same and it worked for me. Most likely you call your service incorrectly, either with the wrong content type or with a malformed JSON payload. Try debugging in Visual Studio if possible and see which part is null. I expect the whole sv.Request.JSONRequest will be null and the real Request will be interpreted as binary or something.

  • 0 in reply to   

    Thanks for the update  , actually I am facing the problem when I try to set the response. example I am trying to get the color value from request and set it to response.

    The response structure looks like below.

    {
    "status": "success",
    "message": "stocks added successfully",
    "stocks": [
    {
    "color": "Blue",
    "name": "YTDD1",
    "itamid": "8740-YTD",
    "status": "added"
    },
    {
    "color": "Red",
    "name": "YTDD2",
    "itamid": "8741-YTD"
    "status":" added
    }
    ]
    }
    


    request you to share the C# code for the reference.

  • 0   in reply to 

    Please be more specific. Tell us what are the inputs, what are the outputs, what's the motivation, what you need to do and I can suggest some code example. But like this I'm afraid I will again answer something you didn't want to ask.

  • 0 in reply to   

    Hi  

    I have the above-mentioned request and response JSON structure imported in data rule,
    1. Based on the stock's items count, inside the for loop I am trying to get the itamid value from the request and
    2. Compare it with some other value and process some logic,
    3. Set the status pass or fail
    4. Also set the color, name & itamid values from request to response

    I am trying to do all the above 4 steps in C# scripted rule. 

  • 0 in reply to 

    Hi  did you get chance look at my above response.

  • 0 in reply to 

    If you are getting NullPointerException when trying to set some structure, than it means that the path you are using has some null node.

    For example if below code throws NPE

    sv.Response.memberSearchResponse.memberSearchResult.Member[0].householdId = 1;

    than memberSearchResponse or memberSearchResult or Member is null. Therefor you need to check for null and create those structures manually before accessing them.

                if (sv.Response.memberSearchResponse == null) {
                    sv.Response.memberSearchResponse = new Type_of_element_memberSearchResponse();
                }
                if (sv.Response.memberSearchResponse.memberSearchResult == null) {
                    sv.Response.memberSearchResponse.memberSearchResult = new ArrayOfMember();
                }
                if (sv.Response.memberSearchResponse.memberSearchResult.Member == null) {
                    sv.Response.memberSearchResponse.memberSearchResult.Member = new Member();
                }
                // because Member is an array, you need to add the first item if you want to access it directly
                if (sv.Response.memberSearchResponse.memberSearchResult.Member.Count == 0) {
                    sv.Response.memberSearchResponse.memberSearchResult.Member.Add(new Member1());
                }
                sv.Response.memberSearchResponse.memberSearchResult.Member[0].householdId = 1;

    (To get a nice code completion for all the types, you can use Visual Studio.)

    alternatively you can use following method, which creates all those structures for you automatically:

    sv.GetOrCreate(x => x.Response.memberSearchResponse.memberSearchResult.Member[0]).householdId = 1;

    above code is actually automatically generated for you if you do Script / Insert Path in the Data Model Editor in SV Designer.