Created
February 10, 2020 15:22
-
-
Save qwertycody/35517403bc851dd2abc7b253367ad263 to your computer and use it in GitHub Desktop.
Example Python Parse of JSON String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This example does NOT account for random colon, comma, or apostrophe values that may be inside of a JSON String Key/Value | |
#This is a starting point for functions that may end up needing to be more complicated | |
#Under no circumstances should you use this outside of an academic forum, highly suggest using the built in JSON library in Python | |
def parse(inputVar): | |
dictionaryToReturn = {} | |
currentValue = '' | |
key = '' | |
value = '' | |
for character in inputVar: | |
if character == ':': | |
key = currentValue | |
currentValue = '' | |
if character == ",": | |
value = currentValue | |
currentValue = '' | |
dictionaryToReturn[key] = value | |
if character != "'" and character != '{' and character != '}' and character != ':' and character != ",": | |
currentValue = currentValue + character | |
dictionaryToReturn[key] = value | |
return dictionaryToReturn | |
def main(): | |
print('Entering Main') | |
input = "{'totalCount':'1','ID':'1029','IP':'10.0.0.1'}" | |
dictionaryToPrint = parse(input) | |
print(dictionaryToPrint) | |
print('Exitting Main') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment