0

I am trying to find the difference between the files and getting the output as well. but if someone could help me on how can i write that to new json file again. Here is my code

def check(a,b):
    diff = False
    for a_key in a:
        if a_key not in b:
            diff = True
            #print "key %s in a, but not in b" %a_key
            print a_key,a[a_key]
        elif a[a_key] != b[a_key]:
            diff = True
            #print "key %s in a and in b, but values differ (%s in a and %s in b)" %(a_key, a[a_key], b[a_key])
            print a_key,b[a_key]
    if not diff:
        print "both files are identical"

I would like to write the output to json file instead of printing on console. I tried this after print but not desired o/p. Any help is appreciated.

res=a_key,b[a_key]
out_file = open("out.json","a")
json.dump(res,out_file, indent=4)
out_file.close()

Here are the Sample files. file1:

{
"abc": [
    "build=1.0.44.0", 
    "proxy=none"
], 
"xyz": [
    "proxy=none", 
    "build=1.0.129.0"
], 
"lmn": [
    "build=1.0.127.0", 
    "proxy=none"
], 
"test": [
    "build=1.0.144.0", 
    "proxy=http"
], 
"alfa": [
    "build=1.0.22.0", 
    "proxy=http"
], 
"beta": [
    "proxy=http",
    "build=1.0.17.0"
]
}

Here is the File2:

{
"abc": [
    "build=1.0.43.0", 
    "proxy=none"
], 
"xyz": [
    "proxy=none", 
    "build=1.0.128.0"
], 
"lmn": [
    "build=1.0.127.0", 
    "proxy=none"
], 
"test": [
    "build=1.0.141.0", 
    "proxy=http"
], 
"alfa": [], 
"beta": [
    "proxy=http",
    "build=1.0.17.0"
]
}

FINAL Expected output:

{
"abc": "1.0.44.0", 
"xyz": "1.0.129.0",
"test":"1.0.144.0", 
"alfa":"1.0.22.0"
}
7
  • 1
    What is the expeted output format Commented Dec 9, 2016 at 13:32
  • What is your desired output? Commented Dec 9, 2016 at 13:37
  • @Backtrack i just updated the desired output Commented Dec 9, 2016 at 13:39
  • @Arundas R updated the desired output Commented Dec 9, 2016 at 13:40
  • @scripting, Check my solution Commented Dec 9, 2016 at 13:50

1 Answer 1

0
A = {"abc":["build=1.0.44.0","proxy=none"],"xyz":["proxy=none","build=1.0.129.0"],"lmn":["build=1.0.127.0","proxy=none"],"test":["build=1.0.144.0","proxy=http"],"alfa":["build=1.0.22.0","proxy=http"],"beta":["proxy=http","build=1.0.17.0"]}

B = {"abc":["build=1.0.43.0","proxy=none"],"xyz":["proxy=none","build=1.0.128.0"],"lmn":["build=1.0.127.0","proxy=none"],"test":["build=1.0.141.0","proxy=http"],"alfa":[],"beta":["proxy=http","build=1.0.17.0"]}


import json
def check(a,b):
    diff = False

    output = {}

    for a_key in a:
            if a_key not in b:
                diff = True
                output[a_key] = a[a_key][0]
            elif a[a_key] != b[a_key]:
                diff = True
                for v in a[a_key]:
                    if 'build=' in v:
                        output[a_key] = v.replace('build=','')
    if not diff:
            print ("both files are identical")
    else:
        print(output)
        with open("output.json","w") as outfile:
            outfile.write(json.dumps(output))


check(A,B)

ouput:

{"xyz": "1.0.129.0", "alfa": "1.0.22.0", "abc": "1.0.44.0", "test": "1.0.144.0"}
Sign up to request clarification or add additional context in comments.

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.