none
Newtonsoft passing Property to a value C# RRS feed

  • Question

  • Hi.

    How can I convert this Json:

    {
      "pnr": "test",
      "lastName": "Perez",
      "body": "",
      "paymentRequests": [
          {
            "paymentMethod": {
            "paymentType": "ExternalPayment"
          }
        }
      ]
    }

    to this Json 

    {
       "pnr":"test",
       "lastName":"Perez",
       "body":{
    	"paymentRequests": [
    		{
    			"paymentMethod": {
    			"paymentType": "ExternalPayment"
    			}
    		}
    	]
    }
    }

    I need to pass the node "paymentRequests" to the value of "body". I dont know how to convert the node to a value from other node.

    Thanks in advance. 


    Friday, May 22, 2020 9:58 PM

Answers

  • One way to do it is to deserialize the original JSON into a class hierarchy that matches the original structure, then copy the data to another set of classes with the target structure, and then serialize the resulting class. Here is an example that uses the NewtonSoft serializer, although the same principle can be used with a different JSON serializer.

    namespace ConvertJson
    {
        using System;
        using Newtonsoft.Json;
    
        class Program
        {
            static void Main(string[] args)
            {
                string example = @"{
                      'pnr': 'test',
                      'lastName': 'Perez',
                      'body': '',
                      'paymentRequests': [
                          {
                            'paymentMethod': {
                            'paymentType': 'ExternalPayment'
                            }
                          }
                        ]
                      }";
    
                InitialData initialData = JsonConvert.DeserializeObject<InitialData>(example);
                TargetData targetData = new TargetData
                {
                    pnr = initialData.pnr,
                    lastName = initialData.lastName,
                    body = new PaymetRequestsContainer { paymentRequests = initialData.paymentRequests }
                };
    
                string result = JsonConvert.SerializeObject(targetData);
                Console.WriteLine(result);
            }
        }
    
        class PaymentTypeContainer
        {
            public string paymentType { get; set; }
        }
    
        class PaymentRequest
        {
            public PaymentTypeContainer paymentMethod { get; set; }
        }
    
        class InitialData
        {
            public string pnr { get; set; }
            public string lastName { get; set; }
            public string body { get; set; }
            public PaymentRequest[] paymentRequests { get; set; }
        }
    
        class PaymetRequestsContainer
        {
            public PaymentRequest[] paymentRequests { get; set; }
        }
    
        class TargetData
        {
            public string pnr { get; set; }
            public string lastName { get; set; }
            public PaymetRequestsContainer body { get; set; }
        }
    }
    

    • Proposed as answer by Rebin Qadir Saturday, May 23, 2020 8:17 PM
    • Marked as answer by diegodamage82 Tuesday, May 26, 2020 2:19 PM
    Saturday, May 23, 2020 12:11 PM
    Moderator