FeatureCollection.fromJson doesn't appear to recognize the layers array

514
4
05-22-2023 11:48 AM
NathanMeade
New Contributor II

When using the example JSON string for featureCollection (https://developers.arcgis.com/web-map-specification/objects/featureCollection/#example), I am able to see FeatureCollection.fromJson work properly. This example string doesn't include the layers array. When I add my own hardcoded layers array to this example JSON string it doesn't appear to work (not listed when calling FeatureCollection.toJson). Any ideas on how to fix this? Here is the example JSON string that I am trying:

{
  "showLegend": false,
  "groupIdField": "GROUPID",
  "groups": [
    {
      "groupId": 1,
      "groupType": "pointSymbolCallout"
    }
  ],
  "layers": [
    {
      "nextObjectId": 1,
	"showLegend": true,
	"popupInfo": {},
	"layerDefinition": {},
	"featureSet": {
	  "features": [],
	  "geometryType": "esriGeometryEnvelope"
	}
    }
  ]
}
0 Kudos
4 Replies
MichaelBranscomb
Esri Frequent Contributor

To confirm, do you mean you create the input JSON, adding one or more layers, call FeatureCollection.FromJSON(), see the sub layers in the feature collection and/or rendered on the map, then call FeatureCollection.FromJSON() and your layers aren't included in the output JSON?

Is it possible the JSON you're inputting doesn't match what the API expects? Have you tried the opposite workflow to validate the JSON, i.e. create the FeatureCollection in code then call ToJson() and compare with the JSON you're inputting?

0 Kudos
NathanMeade
New Contributor II

I am hardcoding a JSON string for the sake of getting this to work like so:

String featureCollectionJsonString = "{\n" +
                "  \"showLegend\": false,\n" +
                "  \"groupIdField\": \"GROUPID\",\n" +
                "  \"groups\": [\n" +
                "    {\n" +
                "      \"groupId\": 1,\n" +
                "      \"groupType\": \"pointSymbolCallout\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"layers\": [\n" +
                "    {\n" +
                "      \"nextObjectId\": 1,\n" +
                "\t\"showLegend\": true,\n" +
                "\t\"popupInfo\": {},\n" +
                "\t\"layerDefinition\": {},\n" +
                "\t\"featureSet\": {\n" +
                "\t  \"features\": [],\n" +
                "\t  \"geometryType\": \"esriGeometryEnvelope\"\n" +
                "\t}\n" +
                "    }\n" +
                "  ]\n" +
                "}";

When I have a working FeatureLayer, if I create a new FeatureCollection.fromJson with that .toJson it doesn't appear to work. There are no tables after doing so. I am not mapping anything, I am only logging out the table data.

0 Kudos
MichaelBranscomb
Esri Frequent Contributor

Can you share the code that shows how you're constructing the FeatureCollection and FeatureCollectionTable(s) before calling ToJson?

 

I just tried a the example from our samples and it works ok:

string FeatureLayerUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/0";
ServiceFeatureTable featTable = new ServiceFeatureTable(new Uri(FeatureLayerUrl));
QueryParameters queryParams = new QueryParameters{WhereClause = "1=1"};
FeatureQueryResult featureResult = await featTable.QueryFeaturesAsync(queryParams);
FeatureCollectionTable collectTable = new FeatureCollectionTable(featureResult);
FeatureCollection featCollection = new FeatureCollection();
featCollection.Tables.Add(collectTable);
await featCollection.LoadAsync();
string json = featCollection.ToJson();
Debug.WriteLine(json);

 

0 Kudos
NathanMeade
New Contributor II

@MichaelBranscomb  Late reply but we ended up taking a different approach for this and we are no longer using JSON. We are now using the geodatabase sync framework now and it is working well for us. I would share the code for this but considering we no longer need to use this JSON functionality we no longer need to spend further investigation into this.