0

can someone shed some lights on how to do this in Python language? There are 2 json documents to compare for the pins direction for the same cell. Each json document will have a list of cell, where each cell having a list of pin list with its respective pin direction, how do I compare the data?

Json 1:
cellA pin1 in
CellA pin2 in
CellA pin3 out

CellB pin1 in
CellB pin2 out

Json 2:
cellA pin1 out
cellA pin2 in
cellA pin3 out

cellB pin1 in

For above 2 cells, Python should indicate mismatches, how should I compare the two? As far, I managed to get each cell on its respective pin and direction but I'm not sure how to compare the two so that in the log show errors in this syntax.

Mismatch [cellA] [pin] [direction_frm_json_1] [direction_frm_json_2]

Thank you in advanced.

Updated for sample json. Json Type 1:

{
"cell_name": "cellA",
"pins": [
    {
        "attributes": [
            "DIRECTION in ;",
            "Comment line ;"
        ],
        "name": "a"
    },
    {
        "attributes": [
            "DIRECTION in ;",
            "Comment line ;"
        ],
        "name": "b"
    },
    {
        "attributes": [
            "DIRECTION out ;",
            "Comment line ;"
        ],
        "name": "o"
    },
    {
        "attributes": [
            "DIRECTION inout ;",
            "Comment line ;"
        ],
        "name": "vcc"
    },
    {
        "attributes": [
            "DIRECTION inout ;",
            "Comment line ;"
        ],
        "name": "vss"
      },
 ],
"sessionid": "grace_test",
"time_stamp": 1505972674.332383,
"file_type": "file1" 
}

Json Type 2:

{'cell_name': 'cellA',
 'power_pin': [{'direction': ['inout'],
                 name': 'vcc',
              },
               {'direction': ['inout'],
                'name': 'vss',
             }],
  'pin': [{'direction': ['out'],
      'name': 'a',
      },
     {'direction': ['in'],
      'name': 'b',
      },
     {'direction': ['out'],
      'name': 'o',
      }],
"sessionid": "grace_test",
"time_stamp": 1505885461.0,
"file_type": "file2"
}
Grace
  • 440
  • 3
  • 6
  • 21

1 Answers1

1

I'm guessing you're working with JSON objects, so you can have keys and values. If that's the case, the first thing to do is parse your documents:

import json
docA = json.loads('{"cellA":{"pin1":"in","pin2":"in","pin3":"out"}, \
                    "cellB":{"pin1":"in","pin2":"out"}}')
docB = json.loads('{"cellA":{"pin1":"out","pin2":"in","pin3":"out"}, \
                    "cellB":{"pin1":"in"}}')

So now you can work with Python data structures (dictionaries in this case). Then you can iterate each dictionary by cell and pin, taking care in case that some cells or pins are missing in one of the documents:

#Check cells in docA
for cell in docA:
    #Check cell pins in docA
    for pin in docA[cell]:
        valueDocB = docB.get(cell,{}).get(pin,None)
        if valueDocB != docA[cell][pin]:
            print("Mismatch",cell,pin,docA[cell][pin],valueDocB)
    #Check cell pins in docB but not in docA
    if cell in docB:
        for pin in set(docB[cell]).difference(set(docA[cell])):
            print("Mismatch",cell,pin,None,docB[cell][pin])
#Check cells in docB but not in docA
for cell in set(docB).difference(set(docA)):
    for pin in docB[cell]:
        print("Mismatch",cell,pin,None,docB[cell][pin])

The output for your example data would be:

Mismatch cellA pin1 in out
Mismatch cellB pin2 out None
stjernaluiht
  • 730
  • 6
  • 14
  • Thank you for your response and sample codes stjernaluiht. The original json come with nested format, does this means I need to use aggregate to restructure the json and feed into a temporary doc before I proceed to check the matching? Is there a way to avoid printing to intermediate doc that I can store in list(or anything else) to compare? – Grace Sep 21 '17 at 03:59
  • Can you also gives sample how to do this: docA = json.loads('{"cellA":{"pin1":"in","pin2":"in","pin3":"out"}, \ "cellB":{"pin1":"in","pin2":"out"}}') – Grace Sep 21 '17 at 04:32
  • I looks around, able to find this link below, but it was loading a file not a json like dictionary. Also, I need to avoid printing anything to intermediate doc. https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file – Grace Sep 21 '17 at 04:33
  • Can you provide an example input JSON? I think I understand that you need to open the JSON documents from a file, right? It is no problem if you don't want the mismatch result printed, it can be stored in a list, saved into a file... – stjernaluiht Sep 21 '17 at 08:35
  • I have 2 types of json to compare. Added in above original question. – Grace Sep 21 '17 at 23:19
  • I have updated as above, apology a bit mess as I am not sure why the code is not indented event I make is 4 space in each initial lines. – Grace Sep 21 '17 at 23:42
  • Your samples are not valid JSON, there are several unmatched brackets. Try asking again with a [MCVE](https://stackoverflow.com/help/mcve) of the problem, I'm unable to help. – stjernaluiht Sep 22 '17 at 15:20
  • Hi stjernaluiht, I have updated the json with the right format, hope you don't mind helping to show how to restructure the json so that I can load to compare. Tq. – Grace Sep 25 '17 at 04:16
  • could you again help to attend to my problem? I am kind of lost track to proceed. Tqvm. – Grace Sep 26 '17 at 01:24
  • You should iterate over the contents of each cell to get all pairs of pin/direction, then print the object as JSON. Sorry but I can't help anymore, I'm having busy times lately. – stjernaluiht Sep 26 '17 at 17:13