Home > Software design >  Pulling data from one JSON and inserting it into a different JSON in c#
Pulling data from one JSON and inserting it into a different JSON in c#

Time:09-21

I have a JSON file that I am pulling from a database, It is in the following format.

{
    "n": [
        {
            "type": "standard.Rectangle",
            "id": "bc037b61-d4a5-4539-b3a9-3eb2183b61dd",
            "x": 275,
            "y": 205,
            "text": "NBN HFC Modem\nGigafy NBN",
            "fill": "#666666",
            "physicalLocation": "",
            "thisInterfaceTypes": [
                1
            ],
            "otherInterfaceTypes": [
                1
            ],
            "link": [
                "1a0757ff-9a6a-413c-ba5a-4461032c7b32"
            ],
            "otherLocationEntityIds": [
                null
            ],
            "shouldUseLegacyLinkTypes": [
                true
            ]
        }
    ]
}

I now need to take the values from that JSON and put them into the following format of JSON.

{
    "cells": [
        {
            "type": "app.Router",
            "size": {
                "width": 90,
                "height": 35
            },
            "position": {
                "x": 170,
                "y": 330
            },
            "angle": 0,
            "id": "987f80af-60be-4a2b-8844-d93a97fea85b",
            "z": 1,
            "attrs": {
                ".card": {
                    "fill": "transparent",
                    "stroke": "#F7B80A",
                    "stroke-dasharray": "0"
                },
                "image": {
                    "width": 42,
                    "height": 42,
                    "ref": null,
                    "ref-x": null,
                    "ref-y": null,
                    "x": 16,
                    "y": 8,
                    "y-alignment": null,
                    "xlink:href": "assets/router2white.svg"
                },
                ".rank": {
                    "text-decoration": "none",
                    "font-family": "Roboto Condensed",
                    "font-size": 10,
                    "text": "Router",
                    "fill": "#f6f6f6",
                    "font-weight": "Bold"
                },
                ".name": {
                    "font-weight": "Normal",
                    "font-family": "Roboto Condensed",
                    "font-size": 10,
                    "text": "IP Address",
                    "fill": "#f6f6f6"
                },
                "root": {
                    "dataTooltipPosition": "left",
                    "dataTooltipPositionSelector": ".joint-stencil"
                }
            }
        }
    ]
}

I am currently using newtonsoft json library, to deserialize and reserialize the JSON, I can currently change the property names and key values how ever my question is,

How can i nest the 'x' and 'y' properties into a 'position' property as shown in the second JSON string. I can currently do it but it will always strip the data and show as null.

here is my class structure

    public class N
{
    public object type { get; set; }
    public object entityId { get; set; }
    public object x { get; set; }
    public object y { get; set; }
    public object text { get; set; }
    public object colour { get; set; }
    public object physicalLocation { get; set; }
    public object[] thisInterfaceTypes { get; set; }
    public object[] otherInterfaceTypes { get; set; }
    public object[] otherDeviceEntityIds { get; set; }
    public object[] otherLocationEntityIds { get; set; }
    public bool[] shouldUseLegacyLinkTypes { get; set; }
    [JsonExtensionData]
    public Dictionary<String, Object> AdditionalData { get; set; }
    [JsonExtensionData]
    public Dictionary<String, Object> AdditionalData1 { get; set; }
    [JsonExtensionData]
    public Dictionary<String, Object> AdditionalData2 { get; set; }
}

any help would be awesome thanks.

EDIT:

In simpler terms, I need to turn this

{"x": 275, "y": 205} 

into

"position":{"x":170,"y":330}

Thanks

CodePudding user response:

If you really have no control over the shape of the incoming data, and you really need the outgoing data to be in a different shape, then you should create an object to deserialize the incoming data, and another one to serialize the outgoing data.

In the object for the outgoing data, you can create a constructor that accepts the incoming object and builds up the outgoing object as desired.

Something like:

using System;
using Newtonsoft.Json;
                    
public class Program
{
    public static void Main()
    {
        const string inJson = @"{""x"": 275, ""y"": 205}";
        var deserialized = JsonConvert.DeserializeObject<InStructure>(inJson);
        var converted = new OutStructure(deserialized);
        var reserialized = JsonConvert.SerializeObject(converted);
        
        Console.WriteLine(reserialized);
        
    }
}

public class InStructure{
    public int x {get;set;}
    public int y {get;set;}
}

public class OutStructure{
    public Position Position{get;set;}
    
    public OutStructure(InStructure inJson){
        this.Position = new Position{
            X = inJson.x,
            Y = inJson.y
        };
    }
}
public class Position{
    public int X {get;set;}
    public int Y {get;set;}
}

output:

{"Position":{"X":275,"Y":205}}

see:

https://dotnetfiddle.net/jbvQjx

I left the property names capitalized, but you can use json property attributes to rename them to what you want.

  • Related