0

ENG_RequestData.json:

{
    "appKey": "9c9fa7201e90d3d96718bc3f36ce4cfe1781f2e82f4e5792996623b3b474fee2c77699eb5354f2136063e1ff19c378f0f6dd984471a38ca5c393801bffb062d6",
    "appId": "NMDPTRIAL_AutomotiveTesting_NCS61HTTP",
    "uId": "Alexander",
    "inCodec": "PCM_16_8K",
    "outCodec": "PCM_16_8K",
    "cmdName": "NVC_TTS_CMD",
    "appName": "Python",
    "appVersion": "1",
    "language": "eng-GB",
    "carrier": "carrier",
    "deviceModel": "deviceModel",
    "cmdDict": {
        "tts_voice": "Serena",
        "tts_language": "eng-GB",
        "locale": "canada",
        "application_name": "Testing Python Script",
        "organization_id": "NUANCE",
        "phone_OS": "4.0",
        "phone_network": "wifi",
        "audio_source": "SpeakerAndMicrophone",
        "location": "47.4925, 19.0513",
        "application_session_id": "1234567890",
        "utterance_number": "5",
        "ui_langugage": "en",
        "phone_submodel": "nmPhone2,1",
        "application_state_id": "45"        
        }
}

CODE :

    print LNG    // it is printing as ENG
    ENG_RequestDataFile = scriptPath + "\\" + "ENG_RequestData.json"
    print ENG_RequestDataFile  // it is printing as C:\Users\\Desktop\OWN\2016_02_11\ENG_RequestData.json
    DEU_RequestDataFile = scriptPath + "\\" + "DEU_RequestData.json"
    try:
        if LNG == 'ENG':
            with open(ENG_RequestDataFile) as json_file:   
                print json_file
                JSON_ENGData = json.load(json_file)
                print JSON_ENGData
        elif LNG == 'DEU':
            with open(DEU_RequestDataFile) as json1_file:    
                JSON_DEUData = json.load(json1_file)
        else:
            print ("No Other Language")
    except:
        print ("[ERROR] Cannot open the Request data file")

I am reading a json file from the specific path and the json file is as shown above. there are two json files for one english and german but I am trying to read, but it is printing as [ERROR] Cannot open the Request data file. I am not able to open it. can someone tell me how ?

sam
  • 203
  • 4
  • 17

1 Answers1

0

Try this code:

import json

#Path you posted
path = os.path.join('C:'+os.sep+'Users'+os.sep+'Desktop'
                    +os.sep+'OWN'+os.sep+'2016_02_11'
                    +os.sep+'ENG_RequestData.json')

def get_tts(LNG,filename):
    try:
        if LNG == 'ENG':
            with open(filename) as json_file:    
                JSON_ENGData = json.load(json_file)
                print(JSON_ENGData)
        elif LNG == 'DEU':
            with open(DEU_RequestDataFile) as json_file:    
                JSON_DEUData = json.load(json_file)
        else:
            print("No Other Language")
    except:
        print("[ERROR] Cannot open the Request data file")

#Execute the function
get_tts('ENG',path)

More information here: https://stackoverflow.com/a/2422864/5915424

Community
  • 1
  • 1
gprad
  • 11
  • 3
  • I am opening the json file in my code using try and except but I am not able to do that. what is the mistake in that ? – sam Feb 11 '16 at 20:01
  • @sam Is the scriptpath variable inside the get_tts function is undefined. – gprad Feb 11 '16 at 20:11
  • it is not coming within try and except loop. can you please tell me why ? the paths are correctz, i checked it by printing in my code. – sam Feb 11 '16 at 20:22
  • Did you import the json module before executing the code? – gprad Feb 11 '16 at 20:31
  • I added import json. Else are you talking something else ? – sam Feb 11 '16 at 20:40
  • I don't see the get_tts function being using anywhere in the script. This function takes the LNG variable. – gprad Feb 11 '16 at 20:53
  • i added the code. but it is printing as No Other Language. – sam Feb 11 '16 at 21:11
  • make sure you pass 'ENG' and not 'eng' to the get_tts function. – gprad Feb 11 '16 at 21:22
  • i saw. I am getting ENG, when I print LNG . I see ENG as the ouput. – sam Feb 11 '16 at 21:25
  • Are you using the get_tts function at all? Not sure how you would get the ENG output for LNG. LNG is a variable used inside the get_tts function. – gprad Feb 11 '16 at 21:33