<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Feature type sort order in REST API using Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/feature-type-sort-order-in-rest-api-using-python/m-p/266874#M20533</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have been working on an issue with Collector and the dropdown box that changes the feature type.&amp;nbsp; Although I carefully sort the domain used for feature types, it always seems to get published with a jumbled order.&amp;nbsp; This can be corrected using the Manage New Features section of AGOL, but this can be tedious and can cause other problems.&amp;nbsp; My best solution was to update the REST API JSON file.&amp;nbsp; See discussion here: &lt;A href="https://community.esri.com/thread/160142" target="_blank"&gt;Feature type sort order in drop-down list&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have since been working on a python script to extract the "types" section of the JSON file, reorder the section and print a text file that can be used in the process.&amp;nbsp; The following script does that.&amp;nbsp; I was just wondering if others had worked on this problem and what solutions they came up with.&amp;nbsp; Any comments on the script are also appreciated (since I'm still learning python).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import json
import collections

# set up input and output files
# input comes from ArcGIS Online REST API JSON file
input_file=open('unordered_types.json', 'r')
output_file=open('ordered_types.json', 'w')

# read 'types' section of input file
# UTF-8 BOM: file starts with \xef\xbb\xbf
# Windows may add BOM, which needs to be removed
result = json.loads(input_file.read().decode("utf-8-sig").encode("utf-8"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object_pairs_hook=collections.OrderedDict)[u'types']

# sort result dictionary by 'id' key
ordered = sorted(result,key=lambda x:x['id'])

# if console output is desired, uncomment next line
# print json.dumps(ordered, indent=4, sort_keys=False)

# convert ordered dictionary back to JSON
back_json=json.dumps(ordered)

# add 'types' to make completed JSON file for REST API
output_file.write('{ "types" : ' + back_json + ' }')

# close output file
output_file.close() 

print 'Done.'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:05:58 GMT</pubDate>
    <dc:creator>RandyBurton</dc:creator>
    <dc:date>2021-12-11T13:05:58Z</dc:date>
    <item>
      <title>Feature type sort order in REST API using Python</title>
      <link>https://community.esri.com/t5/python-questions/feature-type-sort-order-in-rest-api-using-python/m-p/266874#M20533</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have been working on an issue with Collector and the dropdown box that changes the feature type.&amp;nbsp; Although I carefully sort the domain used for feature types, it always seems to get published with a jumbled order.&amp;nbsp; This can be corrected using the Manage New Features section of AGOL, but this can be tedious and can cause other problems.&amp;nbsp; My best solution was to update the REST API JSON file.&amp;nbsp; See discussion here: &lt;A href="https://community.esri.com/thread/160142" target="_blank"&gt;Feature type sort order in drop-down list&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have since been working on a python script to extract the "types" section of the JSON file, reorder the section and print a text file that can be used in the process.&amp;nbsp; The following script does that.&amp;nbsp; I was just wondering if others had worked on this problem and what solutions they came up with.&amp;nbsp; Any comments on the script are also appreciated (since I'm still learning python).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import json
import collections

# set up input and output files
# input comes from ArcGIS Online REST API JSON file
input_file=open('unordered_types.json', 'r')
output_file=open('ordered_types.json', 'w')

# read 'types' section of input file
# UTF-8 BOM: file starts with \xef\xbb\xbf
# Windows may add BOM, which needs to be removed
result = json.loads(input_file.read().decode("utf-8-sig").encode("utf-8"),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object_pairs_hook=collections.OrderedDict)[u'types']

# sort result dictionary by 'id' key
ordered = sorted(result,key=lambda x:x['id'])

# if console output is desired, uncomment next line
# print json.dumps(ordered, indent=4, sort_keys=False)

# convert ordered dictionary back to JSON
back_json=json.dumps(ordered)

# add 'types' to make completed JSON file for REST API
output_file.write('{ "types" : ' + back_json + ' }')

# close output file
output_file.close() 

print 'Done.'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:05:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/feature-type-sort-order-in-rest-api-using-python/m-p/266874#M20533</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-12-11T13:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Feature type sort order in REST API using Python</title>
      <link>https://community.esri.com/t5/python-questions/feature-type-sort-order-in-rest-api-using-python/m-p/266875#M20534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As a short follow up,&amp;nbsp; I copy the text file created by the python script into jsonlint.com and verify the code.&amp;nbsp; If it checks out, I will then use it to update my feature with the REST API.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Jun 2015 19:04:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/feature-type-sort-order-in-rest-api-using-python/m-p/266875#M20534</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2015-06-25T19:04:02Z</dc:date>
    </item>
  </channel>
</rss>

