Home > Software design >  parsing json to extract data using python
parsing json to extract data using python

Time:10-21

All I know about my input is, it's a json. I need to get a src and destination path extracted from it.

The input "text" below is a command line argument. I think text might look like this '{"destination":"/home/usr/item/, "src":"files/"}' If I do this,

  json_dict = json.dumps(text, separators=(" , ", " : "))
  des = json_dict['destination']

I get this error: TypeError: string indices must be integers so I try this,

  json_dict = json.dumps(text, separators=(" , ", " : "))
  des = json_dict[0]

I get one letter 'r' as output

Initially i used json.loads ,

  json_dict = json.loads(text, separators=(" , ", " : "))
  des = json_dict['destination']

I get this error "TypeError: the JSON object must be str, not 'list'"

Anyone who can help out here? Is using json.dumps wrong?

Edit: I tried json_dict = json.loads(json.dumps(text))

json_dict[0] gives me my program name json_dict[1] gives me {"destination":"/home/usr/item/, "src":"files/"}

2nd Edit:

json_dict = json.loads(json.dumps(text))
json_dict = json.loads(json_dict[1])

json_dict[0] was the program name. Now if i try json_dict["src"] I am able to get the src path.

Is this the right way to do this?

CodePudding user response:

From your question, I understand that you want to read in a JSON string from command line and parse that JSON to extract "destination". With this rough understanding of your objective, I think you are using json.dumps and json.loads incorrectly.

  • json.loads() : is used to "load" or read in a JSON string. Usually used to convert a JSON string into a Python object (usually dictionary).
  • json.dumps() : is used to "dump" or write a JSON string. Usually used to convert a Python dictionary object into a string (also known as "serializing the object")

With that basic background, you can read in your text using:

import json
json_dict = json.loads(text)
print(json_dict['destination'])

Since you want to pass this text from command line you would need to pay attention to how you read the command line arguments as well. When passing command line arguments, remember that the first argument (index 0) is always the python file name (.py file) that you run. The actual arguments (in your case text) would start from index 1.

This is how a complete program to read in json string and parse destination can look like:

import sys
import json

if __name__ == "__main__":
    text = sys.argv[1] # sys.argv[0] will be your python program name
    json_dict = json.loads(text)
    print(json_dict['destination'])

Below is a screenshot showing the entire script, how to run it and the result:

enter image description here

  • Related