locked
How can I change this JSON output RRS feed

  • Question

  • User1121128150 posted

    I am using a very simple webapi as follows

     [HttpGet("{orderId}")]
            public IEnumerable<object> Get(string orderId)
            {
                var ohRows = from oh in _context.oh
                             where oh.OrderNumber == orderId
                             select new
                             {
                                 oh
                             };
    
                return ohRows.ToList();
            }

    This produces a JSON output as below

    [{"oh":{"brandId":"1","orderNumber":"1-20170328132246","crmId":"","isSynched":true,"synchDate":"2017-03-28T13:38:12","tillId":1,"userName":"","trDiscountAmount":0.00,"trDiscountPercentage":0.00,"status":10,"orderType":0,"createdDate":"2017-03-28T13:38:12","createdUtcDate":null,"updatedDate":null,"updatedUtCDate":null,"isPosLogged":false}}]

    How can I stop the initial "oh" to be output I want a more flat JSON without that level

    I have another webapi that uses the below code and it does NOT add that extra level in the JSON object

     [HttpGet("{TAXTypeId}")]
            public IEnumerable<object> Get(int TAXTypeId)
            {
                var TAXRate = (from TAXType in _context.TAXType
                                    where TAXType.TAXTypeId == TAXTypeId
                                    select new
                                    {
                                        TAXRate = TAXType.TAXTypeRate
                                    });
    
                return TAXRate;
            }

    This code does NOT have that additional level, instead it produces a flatter JSON object as below which is what I want

    [{"tillId":1,"priceList":2,"campaignPricelist":null,"langId":1,"currencyId":1,"taxTypeId":1,"store":1,"brand":"XX"}]

    Monday, April 10, 2017 8:17 AM

Answers

  • User527778624 posted

    Hi,

    Try updating your code like this:

     [HttpGet("{orderId}")]
            public IEnumerable<object> Get(string orderId)
            {
                var ohRows = from oh in _context.oh
                             where oh.OrderNumber == orderId
                             select oh;
    
                return ohRows;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 10, 2017 8:30 AM