0

I have dictionary formate which contain 4 lists and in the end, I want to convert it into JSON.

modelOutput = {
        "projectYears": 1,
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": []
            }, {
                "name": "variableSavings",
                "variableSavingsCost": []
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": []
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": []
            },
        ],
}

SO if the projectYear value is 2 then there will be 2 dictionaries with 4 lists(Cost) with 2 random values in it in each dictionary. expected output:

#if projectYear = 2
modelOutput = {
        "projectYears": 1,
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": [12,22]
            }, {
                "name": "variableSavings",
                "variableSavingsCost": [11,15]
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": [32,21]
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": [21,33]
            },
        ],
"projectYears": 2,
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": [32,23]
            }, {
                "name": "variableSavings",
                "variableSavingsCost": [23,12]
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": [14,32]
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": [14,13]
            },
        ],

Similarly, if projectYears is 3 then there will 3 dictionaries with 4 lists and 3 values in each of them. I was able to create the random values in the lists according to the projectYears but can't able to form separate dictionaries out of it. My Approach:

projectLife = 3


modelOutput['RevSev'][0]['fixedSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]
modelOutput['RevSev'][0]['variableSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]
modelOutput['RevSev'][0]['startUpSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]
modelOutput['RevSev'][0]['shutDownSavingsCost'] = [random.randrange(1, 50, 1) for i in range(projectLife)]

json.dumps(modelOutput)
7
  • Your question does not contain a question. Commented Jun 24, 2020 at 5:13
  • What do you mean ? I just mentioned that I want dynamic dictionary. I was able to create dynamic list in it but was not able create dynamic dictionary. Commented Jun 24, 2020 at 5:17
  • 1
    The output is not a valid dict object. as it contains the same keys (RevSev, projectYears) multiple times. Are you certain that's what you're looking for? Commented Jun 24, 2020 at 5:19
  • I think you should have an array of dict in the model, rather than having dict with same key which is not valid JSON Commented Jun 24, 2020 at 5:23
  • 1
    So you're probably looking for a list of dictionaries, where each dict contains two entries: ProjectYears and RevSev. Would that make sense? Commented Jun 24, 2020 at 5:37

1 Answer 1

1

Here's a fairly straightforward way of doing that, given modelOutput like the above as input (or even given just the number of years you'd like to have):

years = modelOutput["projectYears"]

randoms = lambda y: np.random.randint(10, 30, y)

res = []
for year in range(1, years+1):
    new_year = {
        "projectYears": year, 
        "RevSev": [
            {
                "name": "FixedSavings",
                "fixedSavingsCost": randoms(years)
            }, {
                "name": "variableSavings",
                "variableSavingsCost": randoms(years)
            }, {
                "name": "startUpSavings",
                "startUpSavingsCost": randoms(years)
            }, {
                "name": "shutDownSavings",
                "shutDownSavingsCost": randoms(years)
            }
        ]
    }
    res.append(new_year)

The result for '2' is:

[{'projectYears': 1,
  'RevSev': [{'name': 'FixedSavings', 'fixedSavingsCost': array([24, 22])},
   {'name': 'variableSavings', 'variableSavingsCost': array([11, 12])},
   {'name': 'startUpSavings', 'startUpSavingsCost': array([27, 22])},
   {'name': 'shutDownSavings', 'shutDownSavingsCost': array([25, 17])}]},
 {'projectYears': 2,
  'RevSev': [{'name': 'FixedSavings', 'fixedSavingsCost': array([15, 19])},
   {'name': 'variableSavings', 'variableSavingsCost': array([20, 13])},
   {'name': 'startUpSavings', 'startUpSavingsCost': array([26, 22])},
   {'name': 'shutDownSavings', 'shutDownSavingsCost': array([24, 25])}]}]
Sign up to request clarification or add additional context in comments.

2 Comments

We can't have that in the dictionary, right? I mean instead of res(which is the dictionary).
You can, change the structure of the output. for example, have a dict in which the key is the year number. I.e {1: {RevSum: {....}}, 2: { ...

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.