<?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 Re: publish() a CSV to Enterprise portal: Decoding JSON has failed in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041406#M60591</link>
    <description>&lt;P&gt;So I managed to publish the table service using parts your code. What versions of the GIS module, Portal and ArcGIS Server are you using?&lt;/P&gt;&lt;P&gt;My assumption is that you are using AGS 10.8.1+ since you are using the item.publish item_id parameter to overwrite.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you are using ArcGIS Server, you can access your server's REST API and perform add contents and analyze by sending a requests.post() to the urls. See if they work individually with your files.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/analyze.htm" target="_blank"&gt;https://developers.arcgis.com/rest/users-groups-and-items/analyze.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm" target="_blank"&gt;https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LongDinh_0-1616973602198.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/9534iD036313602527974/image-size/medium?v=v2&amp;amp;px=400" role="button" title="LongDinh_0-1616973602198.png" alt="LongDinh_0-1616973602198.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 28 Mar 2021 23:30:50 GMT</pubDate>
    <dc:creator>LongDinh</dc:creator>
    <dc:date>2021-03-28T23:30:50Z</dc:date>
    <item>
      <title>publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1040826#M60573</link>
      <description>&lt;P&gt;I'm the same person who posted about &lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/publish-to-enterprise-portal-in-stand-alone-script/m-p/1040658#M5812" target="_self"&gt;this&lt;/A&gt; in the ArcGIS&amp;nbsp;API for Python forum. I'm trying use the publish() method in a stand-alone script. The only documentation I see for it is for the API. I'm able to publish a CSV in a Pro Notebook. But, when I try this in a python IDE the script runs but produces nothing but an error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error is coming from this line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;  }, data=fp)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
import os

gis_dest = GIS('portalurl', 'user', 'pass')
# data
fp = r'C:\Users\jpilbeam\Test\c19_Vaccine.csv'
# content manager
cm = gis_dest.content

# 1. add
item = cm.add(item_properties={
    'type' : 'CSV',
    'title' : 'c19',
    'tags' : 'tags',
    'typeKeywords' : "CSV"
    }, data=fp)

# 2. analyze
analyze_csv = cm.analyze(item=item, file_type='csv')
pp = analyze_csv['publishParameters']

# 3.  Modify the publish parameters then publish
#     For tables ensure the `locationType` is None
pp['locationType'] = 'none'
pitem = item.publish(publish_parameters=pp, file_type='csv')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 21:15:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1040826#M60573</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-03-25T21:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1040878#M60574</link>
      <description>&lt;P&gt;Hi JaredPilbeam2,&lt;/P&gt;&lt;P&gt;The decoder error that appears is from the 'json' module.&lt;/P&gt;&lt;P&gt;What is occurring is the ContentManager is trying to request the data (url/path), convert the response to the&amp;nbsp; JSON format and return it as a Python Dictionary. However, the response from the request fails to decode values from your csv input.&lt;/P&gt;&lt;P&gt;Your csv file may contain JSON keywords that do not decode well in the Python 'json' module. Quite often this is due to a WHERE clause field that can't escape the " or \&amp;nbsp;in a to JSON string correctly.&lt;/P&gt;&lt;P&gt;Some ways you might test this is to try to load your csv to json using pandas. Something like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json
from pandas import pandas as pd

myCSVFileInput = r'C:\csvfile.csv'
myJSONFileOutput= r'C:\jsonfile.json'
# read the file into a pandas dataframe &amp;amp; convert to json
df = pd.read_csv(myCSVFileInput)
df.to_json(myJSONFileOutput)

# Try to decode the new JSON file using json module
with open(myJSONFileOutput) as jFile:
   jsonData = json.load(jFile)
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 23:08:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1040878#M60574</guid>
      <dc:creator>LongDinh</dc:creator>
      <dc:date>2021-03-25T23:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041095#M60580</link>
      <description>&lt;P&gt;Hi LongDinh,&lt;/P&gt;&lt;P&gt;Thanks for the explanation. Does the decode step sort of clean up the json? After using the json module to decode I attempted to convert back to a CSV (because that's the file type I ultimately need). I just tried this real quick, but got an AttributeError.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open(myJSONFileOutput) as jFile:
    jsonData = json.load(jFile)
jsonData.to_csv(r'C:\Users\jpilbeam\Test\decodedCSV.csv', encoding='utf-8', index=False)&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;AttributeError: 'dict' object has no attribute 'to_csv'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EDIT:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I was able to convert that json back to a csv (I think?).&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open(myJSONFileOutput) as jFile:
    jsonData = json.load(jFile)
    df = pd.DataFrame.from_dict(jsonData)
    df.to_csv(r'C:\Users\jpilbeam\Test\decodedCSV.csv', encoding='utf-8', index=False)&lt;/LI-CODE&gt;&lt;P&gt;Then I ran the initial script on this 'decodedCSV.csv'. Same error. I guess I didn't change anything.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 18:01:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041095#M60580</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-03-26T18:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041172#M60581</link>
      <description>&lt;P&gt;In a Pro Notebook if I continuously run the initial code it throws that error only until about the seventh time then it finally works. How's that?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 18:50:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041172#M60581</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-03-26T18:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041214#M60585</link>
      <description>&lt;P&gt;If it matters, here's my whole script. My only option is to publish as a CSV because in the first part I write requested JSON to a CSV. If I run this I'll get the 'Decoding JSON has failed' error (even in a try/except) and the CSV will neither be added nor published.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json,requests
from arcgis.gis import GIS
import os,sys
import csv
from datetime import datetime

#the directory you want the csv written to
file_path = r'C:\Users\jpilbeam\Test'
#file name
csv_file = 'c19_VaccineTest'
##idph url goes here
idph_data = 'https://idph.illinois.gov/DPHPublicInformation/api/covidVaccine/getVaccineAdministrationCurrent'

#1----------------this section writes the json to a csv--------------

full_file = os.path.join(file_path, csv_file) + str(datetime.now().strftime('_%m_%d_%Y_%H%M')) + '.csv'

#get json data from idph
response = requests.get(idph_data,verify=True)
#read the json response and keep the VaccineAdministration part
data = response.json()['VaccineAdministration']

#write to file
with open(full_file, 'w', newline='', encoding='UTF-8') as csvfile:
    f = csv.writer(csvfile) 
    #write the headers of the csv file
    f.writerow(['County','AdminCount','AdminCountChange', 'RollAvg', 'AllocDoses', 'FullyVaccinated', 'FullyVaccinatedChange', 'ReportDate', 'Pop', 'PctVaccinated', 'LHDInventory', 'CommInventory','TotalInventory', 'InventoryDate'])
    for elem in data:
        #get the values for all the keys (i.e. CountyName, AdministeredCount, etc...)
        f.writerow([elem['CountyName'], elem['AdministeredCount'], elem['AdministeredCountChange'], elem['AdministeredCountRollAvg'], elem['AllocatedDoses'], elem['PersonsFullyVaccinated'], elem['PersonsFullyVaccinatedChange'], elem['Report_Date'], elem['Population'], elem['PctVaccinatedPopulation'], elem['LHDReportedInventory'], elem['CommunityReportedInventory'], elem['TotalReportedInventory'], elem['InventoryReportDate']])
    
print('csv successfully written')

#2------------this section takes that csv and publishes it to Enterprise Portal--------------

#provide Enterprise Portal login info here
gis_dest = GIS('portalURL', 'user', 'pass', verify_cert=False, trust_env=True)
#content manager
cm = gis_dest.content

print('publishing to Enterprise Portal...')
try:
    #add the CSV file
    item = cm.add(item_properties={
        'type' : 'CSV',
        'title' : 'C19VaccineTest',
        'tags' : 'test',
        'typeKeywords' : "CSV"
        },
           data=full_file)

    #analyze the CSV file to autogenerate the publish parameters
    analyze_csv = cm.analyze(item=item, file_type='csv')
    pp = analyze_csv['publishParameters']

    #Modify the publish parameters 
    #for tables ensure the `locationType` is None
    pp['locationType'] = 'none'
    itemid = item.id

    #Publish
    item.publish(publish_parameters=pp, overwrite=True, file_type='csv', item_id=itemid)
   
   
except ValueError as e:  # includes json.decoder.JSONDecodeError
    print(f'Decoding JSON has failed: {e}')

print("---done---")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 20:31:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041214#M60585</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-03-26T20:31:44Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041406#M60591</link>
      <description>&lt;P&gt;So I managed to publish the table service using parts your code. What versions of the GIS module, Portal and ArcGIS Server are you using?&lt;/P&gt;&lt;P&gt;My assumption is that you are using AGS 10.8.1+ since you are using the item.publish item_id parameter to overwrite.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you are using ArcGIS Server, you can access your server's REST API and perform add contents and analyze by sending a requests.post() to the urls. See if they work individually with your files.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/analyze.htm" target="_blank"&gt;https://developers.arcgis.com/rest/users-groups-and-items/analyze.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm" target="_blank"&gt;https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LongDinh_0-1616973602198.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/9534iD036313602527974/image-size/medium?v=v2&amp;amp;px=400" role="button" title="LongDinh_0-1616973602198.png" alt="LongDinh_0-1616973602198.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Mar 2021 23:30:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041406#M60591</guid>
      <dc:creator>LongDinh</dc:creator>
      <dc:date>2021-03-28T23:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041542#M60594</link>
      <description>&lt;P&gt;Hi LongDinh,&lt;/P&gt;&lt;P&gt;That's great you were able to publish. If you don't mind I'd like to pick your brain a bit more. I'm using GIS module/Python API v. 1.8.4, Portal v. 10.8.&lt;/P&gt;&lt;P&gt;Would it be possible for you to post your code so I can see what you did? I'm looking into sending a requests.post(). Yes, I'm using ArcGIS Server v. 10.8.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Mar 2021 14:57:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1041542#M60594</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-03-29T14:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1045803#M60736</link>
      <description>&lt;P&gt;Hi JaredPilbeam2,&lt;/P&gt;&lt;P&gt;Apologies for the delayed response - was on a break. The only thing I changed with my code was the item_id and overwrite kwargs in item.publish as I am using AGS 10.7.1 which does not accept them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;item.publish(publish_parameters=pp, file_type='csv')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this isn't working, it might be something to do with your Portal Installation - I'm not really sure how to debug this issue.&lt;/P&gt;&lt;P&gt;In regards to sending requests.post() manually, it is a bit harder. You will need to determine which REST APIs to use (portal and/or admin).&lt;/P&gt;&lt;P&gt;For the arcigs.GIS implementation as Python REST Requests,&amp;nbsp;(not exactly sure but) you can call the following&amp;nbsp; :&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;/Analyze/POST,&lt;/LI&gt;&lt;LI&gt;/user/{username}/AddItem, or /user/{username}/PublishItem (I think it's the latter)&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;See notes:&amp;nbsp;&lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm" target="_blank" rel="noopener"&gt;https://developers.arcgis.com/rest/users-groups-and-items/add-item.htm&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&amp;nbsp;&lt;A href="https://developers.arcgis.com/rest/users-groups-and-items/analyze.htm" target="_blank" rel="noopener"&gt;https://developers.arcgis.com/rest/users-groups-and-items/analyze.htm&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your requests for #1 should look be something the below. It should work with your current codeblock which creates full_file path&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests 

analyzeUrl = "https://domain.gis.com/arcgis/sharing/rest/content/features/analyze"

payload={'filetype': 'csv',
'f': 'json'}
files=[
  ('file',(os.path.basename(full_file),open(full_file),'text/csv'))
]
headers = {}

response = requests.request("POST", analyzeUrl, headers=headers, data=payload, files=files)

print(response.text)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I highly recommend using Postman - an application for HTTP/S request management. Postman will also allow you to convert your HTTPS request to Python and other languages.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Apr 2021 03:06:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1045803#M60736</guid>
      <dc:creator>LongDinh</dc:creator>
      <dc:date>2021-04-12T03:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: publish() a CSV to Enterprise portal: Decoding JSON has failed</title>
      <link>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1078690#M61745</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/180019"&gt;@LongDinh&lt;/a&gt;Thanks for the help. This was recently resolved. After a lot of trouble shooting, I still don't know that we (an ESRI analyst and I) nailed down a cause of that JSONDecode error because we tried so many things. But, this at least may be informative:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;When running a Python script that is set up to add then publish an item to Portal for ArcGIS the POST request to the /addItem endpoint of the Portal is being redirected, which causes the script to crash.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;What we ultimately came up with is a two part script that publishes a CSV to Portal,&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json,requests
from arcgis.gis import GIS
import os,sys
import csv
from datetime import datetime

#the directory you want the csv written to
file_path = r'C:\Users\jpilbeam\Test'
#file name
csv_file = 'c19_Vaccine_Current_standAloneScriptTest'
##idph url goes here
idph_data = 'https://idph.illinois.gov/DPHPublicInformation/api/covidVaccine/getVaccineAdministrationCurrent'


#1----------------this section writes the json to a csv--------------

full_file = os.path.join(file_path, csv_file) + '.csv'

#get json data from idph
response = requests.get(idph_data)
#read the json response and keep the VaccineAdministration part
data = response.json()['VaccineAdministration']

#write to file
with open(full_file, 'w', newline='', encoding='UTF-8') as csvfile:
    f = csv.writer(csvfile) 
    #write the headers of the csv file
    f.writerow(['County','AdminCount','AdminCountChange', 'RollAvg', 'AllocDoses', 'FullyVaccinated', 'FullyVaccinatedChange', 'ReportDate', 'Pop', 'PctVaccinated', 'LHDInventory', 'CommInventory','TotalInventory', 'InventoryDate', 'Latitude', 'Longitude'])
    for elem in data:
        #get the values for all the keys (i.e. CountyName, AdministeredCount, etc...)
        f.writerow([elem['CountyName'], elem['AdministeredCount'], elem['AdministeredCountChange'], elem['AdministeredCountRollAvg'], elem['AllocatedDoses'], elem['PersonsFullyVaccinated'], elem['PersonsFullyVaccinatedChange'], elem['Report_Date'], elem['Population'], elem['PctVaccinatedPopulation'], elem['LHDReportedInventory'], elem['CommunityReportedInventory'], elem['TotalReportedInventory'], elem['InventoryReportDate'], elem['Latitude'], elem['Longitude']])
    
print('csv successfully written')


#2------------this section takes that csv and publishes it to Enterprise Portal--------------

#provide Enterprise Portal login info here
gis_dest = GIS("home")

#content manager
cm = gis_dest.content

#add the CSV file
status = 1
while status &amp;lt;= 1:
    try:
        item = cm.add(item_properties={
            'type' : 'CSV',
            'title' : 'CoVID19_csvtest',
            'tags' : 'test',
            'typeKeywords' : "CSV"
            },
                data=full_file)
        status = 2
        print("Item added successfully.")
    except:
        print("Item failed to add, trying again...")
        status = 0
    

#analyse/publish the CSV file
status = 1
while status &amp;lt;= 1:
    try:
        #analyze the CSV file to autogenerate the publish parameters
        analyze_csv = cm.analyze(item=item, file_type='csv')
        pp = analyze_csv['publishParameters']
        #Publish
        pitem = item.publish(publish_parameters=pp)
        print(pitem.layers)
        print("---done---")
        status = 2
    except:
        print("Item publishing failed, trying again...")
        status = 0&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and because this is run on a regular basis the feature layer needs to be overwritten&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json,requests
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
import os,sys
import csv
from datetime import datetime

#the directory you want the csv written to
file_path = r'C:\Users\jpilbeam\Test'
#file name
csv_file = 'c19_Vaccine_Current_standAloneScriptTest'
##idph url goes here
idph_data = 'https://idph.illinois.gov/DPHPublicInformation/api/covidVaccine/getVaccineAdministrationCurrent'


#1----------------this section writes the json to a csv--------------

full_file = os.path.join(file_path, csv_file)+ '.csv'
if os.path.exists(full_file):
    os.remove(full_file)

#get json data from idph
response = requests.get(idph_data)
#read the json response and keep the VaccineAdministration part
data = response.json()['VaccineAdministration']

#write to file
with open(full_file, 'w', newline='', encoding='UTF-8') as csvfile:
    f = csv.writer(csvfile) 
    #write the headers of the csv file
    f.writerow(['County','AdminCount','AdminCountChange', 'RollAvg', 'AllocDoses', 'FullyVaccinated', 'FullyVaccinatedChange', 'ReportDate', 'Pop', 'PctVaccinated', 'LHDInventory', 'CommInventory','TotalInventory', 'InventoryDate', 'Latitude', 'Longitude'])
    for elem in data:
        #get the values for all the keys (i.e. CountyName, AdministeredCount, etc...)
        f.writerow([elem['CountyName'], elem['AdministeredCount'], elem['AdministeredCountChange'], elem['AdministeredCountRollAvg'], elem['AllocatedDoses'], elem['PersonsFullyVaccinated'], elem['PersonsFullyVaccinatedChange'], elem['Report_Date'], elem['Population'], elem['PctVaccinatedPopulation'], elem['LHDReportedInventory'], elem['CommunityReportedInventory'], elem['TotalReportedInventory'], elem['InventoryReportDate'], elem['Latitude'], elem['Longitude']])
    
print('csv successfully written')


#2------------this section takes that csv and publishes it to Enterprise Portal--------------

#provide Enterprise Portal login info here
gis_dest = GIS("home")

#Get published item's ID to overwrite
hostedFeatureService = gis_dest.content.get('5c770f0e208d4504872a7e4b2a825124')
hostedFeatureLayerCollection = FeatureLayerCollection.fromitem(hostedFeatureService)

#overwrite existing HFL contents
status = 1
while status &amp;lt;= 1:
    try:
        result = hostedFeatureLayerCollection.manager.overwrite(full_file)
        print('layer overwritten')
        print('---done---')
        status = 2
    except:
        print("Layer failed to overwrite, trying again...")
        status = 0&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jul 2021 14:21:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/publish-a-csv-to-enterprise-portal-decoding-json/m-p/1078690#M61745</guid>
      <dc:creator>JaredPilbeam2</dc:creator>
      <dc:date>2021-07-14T14:21:10Z</dc:date>
    </item>
  </channel>
</rss>

