kleinibird
kleinibird

Reputation: 19

Modify JSON to Combine 2 objects into an Array

Very new to C# here. I have a list of points in a JSON file, where the geometry is stored like:

{
  "displayFieldName" : "",
  "fieldAliases" : {
    "FID" : "FID",
    ...
  },
  "geometryType" : "esriGeometryPoint",
  "spatialReference" : {
    "wkid" : 4326,
    "latestWkid" : 4326
  },
  "fields" : [
    {
      "name" : "FID",
      "type" : "esriFieldTypeOID",
      "alias" : "FID"
    },
    {
      "name" : "LAYER",
      "type" : "esriFieldTypeString",
      "alias" : "LAYER",
      "length" : 21
    },
    ...
  ],
  "features" : [{"attributes":{"FID":0,"LAYER":"Unknown Point Feature",..."geometry":{"x":10.050555968000026048,"y":116.21585196500007}}
..]

And have been trying to reformat the x and y objects into a single array like so:

"geometry": [123.095215893000045,20.11883803400008]

Have been toying around with Newtonsoft.Json with no luck

Any suggestions?

Edit: As suggested in the comments, I updated the abbreviated/general layout of the JSON file... It was an ESRI shapefile exported as JSON. Thanks to Serge's guidance I was able to figure it out. Snippet below:

string json = File.ReadAllText("file.json");
var jsonParsed = JObject.Parse(json);

foreach (var item in JArray.Parse(jsonParsed["features"].ToString()))
{
    var geometry = item["geometry"];      
    jsonParsed["geometry"] = new JArray() { geometry["x"], geometry["y"] };   
}

Upvotes: 1

Views: 53

Answers (1)

Serge
Serge

Reputation: 43860

You have to parse your json at first

  var jsonParsed=JObject.Parse(json);
  var geometry = (JObject)jsonParsed["geometry"];

  jsonParsed["geometry"] = new JArray() { geometry["x"],geometry["y"] };

  json = jsonParsed.ToString();

json

{
  "geometry": [
    123.09521589300005,
    20.11883803400008
  ]
}

Upvotes: 3

Related Questions