1

I am a beginner in Python. I am migrating from visual basic.

I have multiple .json files that I want to convert them all in one script using python to a file Geodatabase:

What is the issue in the following code

    # Esri start of added imports
    import sys, os, arcpy
    import arcpy
    import os.path
    import os
 
   try:
for x in range(100):
  
    gdb=arcpy.GetParameterAsText(0)
    arcpy.env.workspace = "C:\buildings_cabinets"
    arcpy.JSONToFeatures_conversion("C:\buildings_cab\ x + ".json", 
    os.path.join("C:\buildings_cab\yu.gdb", x + ".json))
   

 except Exception as e:
    arcpy.AddMessage(e.message)
2

Inside your try block, change it to the following:

gdb=arcpy.GetParameterAsText(0)
arcpy.env.workspace = "C:/buildings_cabinets"
arcpy.JSONToFeatures_conversion("C:/buildings_cab/{}.json".format(x),
os.path.join("C:/buildings_cab/yu.gdb", x))

What did I change?

  1. I used a format string to help dynamically populate your first argument in arcpy.JSONToFeatures_conversion(). "C:/buildings_cab/{}.json".format(x) is processed into C:/buildings_cab/x.json" which I believe is what you were looking for. You were on the right track using string concatenation with +, but you need to make sure you have properly terminated each substring with another ". For example: "C:/buildings_cab/" + x + ".json". Either way works, but I used a format string syntax for 'cleaner code'.
  2. I replaced your \ into /. Why is this important? A single \ used within a string will combine with some characters to form an escape character to allow us to inject an 'illegal' character (i.e. has a special function) such as " into a string. Using a \ in a string will check to see if the immediately following character combines with it to form one of these escape characters. This is fine if the characters following your \ don't create one of these escape characters or you intended to use it as an escape character, but in the case of your code \b is the escape character for backspace. If you try printing "C:\building_cab\yu.gdb" for instance, you will get C:\x08uilding_cab\\yu.gdb where x08 is the UTF-8 sequence for backspace used in a string. If you wish to use \ in your file paths, it is good form to use a raw string. Simply prefix your string with an r and it will ignore this special use of \ and include it literally in your string. Example r"C:\building_cab\yu.gdb"
  3. Lastly, I fixed your path join in your final line. You cannot insert a .json file into a geodatabase, so the parameter is not looking for a .json, rather a Feature Class. Feature classes have no extensions, so you can simply join "C:/building_cab/yu.gdb" and x to get the feature class C:/building_cab/yu.gdb/x.

Note: I did not test the code, but hopefully it gets you going.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.