locked
How to read nested Json Data RRS feed

  • Question

  • User-1327394822 posted

    my url receives data through a shopify webhook... I am unable to assign nested Json data to a variable..

    Also can someone improve the below code and respond as status 200 ok... is what is have done correct?

    Following is the JSON by shopify

    {
      "id": 327474488104976385,
      "email": "example@email.com",
      "customer": {
        "id": 1234567,
        "phone": null,
        "tags": "",
        "last_order_name": null
      }
    }

    my Code:

    @using System.Web.Script;
    @using System.Web.Script.Serialization;
    
    
    @{
        string data = new StreamReader(Request.InputStream).ReadToEnd().ToString().Trim();
        var jsonSerializer = new JavaScriptSerializer();
        dynamic hpObj = jsonSerializer.Deserialize<dynamic>(data);
        var id = hpObj["id"];
        var id2 = hpObj["customer"]["phone"];
        var text = "Dear xxx, thank you for your order. Your order will be dispatched shortly. " + id;
        SMS.sendSMS("xxx", text);
        Response.StatusCode = 200;
    }

    Var id was assigned successfully... buy id2 is null... 

    How to assign the "phone" property under "customer" to id2 variable.

    Wednesday, April 12, 2017 1:39 PM

Answers

  • User475983607 posted

    Gautam Sharma

    Var id was assigned successfully... buy id2 is null... 

    Correct, because you specifically assigned the value for phone, which is null, to var id2.

    var id2 = hpObj["customer"]["phone"];

    The following syntax retrieves the customer.id. 

    var id2 = hpObj["customer"]["id"];

    Use the Visual Studio debugger to step through your code and view that state of variables.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 12, 2017 1:53 PM
  • User-1509636757 posted

    How to assign the "phone" property under "customer" to id2 variable.

    what you are doing is correct way for accessing dynamics. id2 is null because the value itself is null in your json response:

    var id2 = hpObj["customer"]["phone"];
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 12, 2017 1:58 PM

All replies

  • User475983607 posted

    Gautam Sharma

    Var id was assigned successfully... buy id2 is null... 

    Correct, because you specifically assigned the value for phone, which is null, to var id2.

    var id2 = hpObj["customer"]["phone"];

    The following syntax retrieves the customer.id. 

    var id2 = hpObj["customer"]["id"];

    Use the Visual Studio debugger to step through your code and view that state of variables.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 12, 2017 1:53 PM
  • User-1509636757 posted

    How to assign the "phone" property under "customer" to id2 variable.

    what you are doing is correct way for accessing dynamics. id2 is null because the value itself is null in your json response:

    var id2 = hpObj["customer"]["phone"];
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 12, 2017 1:58 PM
  • User-1327394822 posted

    Is the Response.Status = 200 ok? because I am receiving continuous requests from shopify even after ok 200

    Wednesday, April 12, 2017 2:02 PM
  • User-1327394822 posted
    Response.StatusCode = 200;

    Is this correct too? Somehow shopify keeps posting data... any other way to set response to 200

    Wednesday, April 12, 2017 2:03 PM