1

So, I am attempting to search a json file using python and am completely stuck. I will probably get ragged for this, but I have nowhere else to turn. I am a python novice and have been unable to find the info I need by searching....

Here is what I am attempting to do: I have a constant, called A1, the same length as X1, but a different value. I wish to divide my constant by each subsequent X1 (located in the json file). When there is a remainder (modulo) of more than 2, I wish to print the X1 value. Basically I am attempting to find the GCD of my constant and every X1, but the gcd will typically only be 1. I only wish to print when the modulo is greater than 1.

Here is my json file:

{
  “536723876acbdacbd3344”: {
    “X1”: "0x2345678abcdef1”,
    “X2”: "0x12345678abcde"
  },
  “7632948974879abcdabcd”: {
    “X1”: "0x1234678abcdef”,
    “X2”: "0x12345678abcde"
 },
  “23847298347233abcdabcd”: {
    “X1”: "0x1234678abcddd”,
    “X2”: "0x12345678abcde"
}
}

Here is the code I've written so far:

import json

data = json.load(open("test1.json"))

def translate(w):
    A1 = int("0xffff123123", 16)
    if w in data.values:
        X1 = int("w", 16)
        if A1%X1 > 2:
            print("The modulo is", data.values[X1])
        else:
            print( "No match")

Any help would be nice. I'm having a hard way finding the information I need by searching. Thank you!

4
  • Keep in mind that dictionaries in python do not come in order. So expecting data to be subsquent will be a huge mistake. You can use Ordereddict module or have the JSON give you a list of values (this can include a list of dictionaries) and those will stay in order. Commented Oct 22, 2018 at 14:44
  • 1
    I think it's better to take from the json object a json array and then loop on each item. Do you try this approach? Commented Oct 22, 2018 at 14:46
  • Has your JSON to be a single JSON object or it can be a JSON array? Commented Oct 22, 2018 at 14:54
  • I'm sorry, I think I misrepresented part of my goal. I simply wish to compare my constant A1 with each X1 value and print the gcd. I do not need to search in any particular order. As long as I check each X1, it works. Commented Oct 22, 2018 at 14:55

2 Answers 2

1
import json

data=json.load(open("file.json"))

for key in data:
    A1 = int("0xffff123123", 16)
    obj = data.get(key)
    x1 = obj.get('X1')
    x1 = int(x1, 16)
    if A1%x1 > 2:
        # print what you need
        print("Module greater than 2")
    else:
        print("No match")

I don't know if it's exactly what you need, but in this way in x1 you have the value of the field X1 for each JSON object. Is up to you add the correct logic to the program (if statement).
Pay attention: you paste a JSON in which you have characters different from doublequotes (").

Sign up to request clarification or add additional context in comments.

Comments

1

I would shape your code like this:

import json

A1 = int("0xffff123123", 16)

with open('test1.json') as f:
    data = json.load(f)
    for key in data:
        value = data.get(key)
        X1 = int(value.get("X1"), 16)
        if A1%X1 > 2:
            print("The modulo is greater then 2")
        else:
            print("No match")

Notice that the steps are: open the file, load the JSON, loop the objects doing your task.

Is also important you check your JSON string since some of the double quotes in there are not really double quotes and this surely will impact your run

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.