0
data1= [open("lis1.json", 'r')]
data2= [open('list2.json', 'r')]


first_set = set(map(tuple, data1))
second_set = set(map(tuple, data2))


x= (first_set-second_set)

print(x)

I am attempting to view two list and get the data in list 1 that are not in list two. the issue is list one format for both set of data is as below :

data1= ["Jane Doe", 98132]["John Doe", 12345]

or

data1=
[
    "jane doe",
    98132
][
    "john doe",
    12345
]
   data2=["Jane Doe", 98132]["John drew", 8989]["John drew", 8989]["John drew", 8989]
data2= 
[
    "jane doe",
    98132
][
    "john drew",
    8989
]

I was expecting

x = [["John Doe", 12345]

I am comparing a list of users vs call logs so it might have duplicate.


in the file I am seeing this in the errors :"Json standard allows only one top-level value"


if I use json(load)

Traceback (most recent call last):
  File "/Users/diff.py", line 4, in <module>
    data1 = json.load(open("list1.json"))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 19 (char 18)
Barmar
  • 741,623
  • 53
  • 500
  • 612
EJC
  • 1
  • 1
  • 1
    You need to use `json.load()` to parse the JSON in the files. You're comparing them as strings, not lists. – Barmar Apr 10 '23 at 17:39
  • And what DID you get? That particular example isn't going to work because `data1` is a list that contains a file handle. It's not a list of strings. – Tim Roberts Apr 10 '23 at 17:39
  • @TimRoberts `tuple(file_object)` will read the file and make a tuple of all the lines. – Barmar Apr 10 '23 at 17:42
  • This should work for you: https://stackoverflow.com/questions/37422290/difference-of-two-sets-of-tuples-in-python – Bouji Apr 10 '23 at 17:44
  • related: https://stackoverflow.com/questions/32815640/how-to-get-the-difference-between-two-dictionaries-in-python – JonSG Apr 10 '23 at 17:47

1 Answers1

2

You need to use json.load() to parse the JSON.

data1 = json.load(open("list1.json"))
data2 = json.load(open("list2.json"))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I am getting a parsing error due to the fact that the data has [] brackets as oppose to {} anything I can do either change the data or properly convert it – EJC Apr 10 '23 at 20:29
  • That shouldn't cause a parsing error. `[]` is used for creating lists, `{}` is for dictionaries. Can you update the question and add a sample of the JSON file? – Barmar Apr 10 '23 at 20:31
  • Hello Barmar, thanks for your help and I have added more information – EJC Apr 11 '23 at 03:38
  • Your files are not proper JSON. I think you've written multiple JSONs to the same file, instead of combing everything into an array and then writing it as a single JSON. – Barmar Apr 11 '23 at 15:12
  • The JSON should look like `[["Jane Doe", 98132],["John Doe", 12345]]` – Barmar Apr 11 '23 at 15:13