1

I'm new to python. I'm trying to extract data from data.json file.

How can i get "Files_Names" and "project_name"? Also, how to manipulate data, "XX\XX\X" is extra string.

desire output:

File_Names = ih/1/2/3.java
             ihh/11/22/33.java.java
Project_name = android/hello
File_Names = hi/1/2/3.java
             hih/11/22/33.java.java
Project_name = android/helloworld

data.json

{
    "changed": [
        {
            "prev_revision": "a09936ea19ddc9f69ed00a7929ea81234af82b95", 
            "added_commits": [
                {
                    "Lines_Deleted": 28, 
                    "File_Names": [
                        "1\t3\tih/1/2/3.java", 
                        "1\t1\tihh/11/22/33.java.java"
                    ], 
                    "Files_Modified": 8, 
                    "Lines_Inserted": 90
                }
            ], 
            "project_name": "android/hello"
        }, 
       {
            "prev_revision": "a09936ea19ddc9f69ed00a7929ea81234af82b95", 
            "added_commits": [
                {
                    "Lines_Deleted": 28, 
                    "File_Names": [
                        "14\t3\thi/1/2/3.java", 
                        "1\t1\thih/11/22/33.java.java"
                    ], 
                    "Files_Modified": 8, 
                    "Lines_Inserted": 90
                }
            ], 
            "project_name": "android/helloworld"
        }

    ]
}
1
  • I was looking for how to deal with sub_dict entry. Commented Aug 20, 2018 at 20:49

3 Answers 3

3

import json then use json.load(open('data.json')) to read the file. It will be loaded as a nested hierarchy of python objects (dictionaries, lists, ints, strings, floats) which you can parse accordingly.

Here's something to spark your imagination and communicate the concept.

import json
x = json.load(open('data.json'))
for sub_dict in x['changed']:
    print('project_name', sub_dict['project_name'])

    for entry in sub_dict['added_commits']:
        print (entry['File_Names'])
Sign up to request clarification or add additional context in comments.

1 Comment

I was getting stuck on "sub_dict".. this is very helpful. Thank you!
2

You can use this approach

import json

with open('data.json') as json_file: 
    data = json.loads(json_file)
    for item in data['changed']:
        print(item['project_name'], item['added_commits']['File_Names'])

Comments

1

You can use something like this with json module

import json
f = open("file_name.json", "r")
data = f.read()
jsondata = json.loads(data) 
print jsondata # all json file 

print jsondata["changed"] # list after dictionary 

print jsondata["changed"][0] # This will get you all you have in the first occurence within changed
 f.close()

From here you can take it further with whatever elements you want from the json.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.