0

I'm trying to change values in a JSON file from strings to integers, my issue is that the keys are row numbers so I can't call by key name (as they will change consistently). The values that need changing are within the "sharesTraded" object. Below is my JSON file:

{
    "lastDate": {
        "30": "04/04/2022",
        "31": "04/04/2022",
        "40": "02/03/2022",
        "45": "02/01/2022"
    },
    "transactionType": {
        "30": "Automatic Sell",
        "31": "Automatic Sell",
        "40": "Automatic Sell",
        "45": "Sell"
    },
    "sharesTraded": {
        "30": "29,198",
        "31": "105,901",
        "40": "25,000",
        "45": "1,986"
    }
}

And here is my current code:

import json

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

dataa = data['sharesTraded']
dataa1 = dataa.values()
data1 = [s.replace(',', '') for s in dataa1]
data1 = [int(i) for i in data1] 


open("AAPL22_trades.json", "w").write(
    json.dumps(data1, indent=4))

However, I need the integer values to replace the string values. Instead, my code just replaces the entire JSON with the integers. I imagine there is something extra at the end that I'm missing because the strings have been changed to integers but applying it to the JSON is missing.

1
  • Why do you write out data1 back to the file? Surely you meant to modify just the nested dict within data, and then just write data back to the file (through json.dump()) Commented Dec 4, 2022 at 17:08

1 Answer 1

2

You have a couple of problems. First, since you only convert the values to a list, you loose the information about which key is associated with the values. Second, you write that list back to the file, loosing all of the other data too.

You could create a new dictionary with the modified values and assign that back to the original.

import json

data = json.load(open("AAPL22_trades.json"))
data['sharesTraded'] = {k:int(v.replace(',', '')) 
    for k,v in data['sharesTraded'].items()}
json.dump(data, open("AAPL22_trades.json", "w"), indent=4)
Sign up to request clarification or add additional context in comments.

5 Comments

Did you mean: json.dump(data, open("AAPL22_trades.json", "w"), indent=4)?
@quamrana - oh, right. The actual data. Fixed.
btw do you regard having open() as a parameter, just a hack or a valid alternative to with open(...?
@quamrana - yes, I'm old-school that way. File objects close the underlying file when they are deleted. Here, I'm trusting that json.dump doesn't keep a reference to the file, so it closes when the function exits. Implementations like the now-defunct jython do background garbage collection so the file object isn't really deleted when the function exits. And in a more complicated case, you may have circular references that get the python garbage collector involved, with the same problematic result. But when doing things where you don't have those risks, I'm comfortable skipping with.
@quamrana - Most python objects are deleted when refcount goes to zero. Garbage collection happens when refcount goes wrong. And to me, that is a strong suggestion that you've got a bug that should be addressed. There are legitimate cases where python can't call an object's __del__, which is good argument for using with. But if __del__ isn't called, that's crud building up in your program which would be a problem for anything long-running.

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.