1

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"] };   
}
2
  • 1
    It's always nicer if the code you provide is valid. That JSON snippet is invalid (it starts with a closing curly bracket and a comma) and, even ignoring that, does represent "a list of geometric points in a JSON file" Commented Aug 24, 2022 at 15:42
  • Can you give a full sample of your JSON, as what you have is just a fragment Commented Aug 24, 2022 at 18:39

1 Answer 1

3

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
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.