0

I am trying to write a code that reads the very outside of the json file data.json, which would output the outside of it. [RSI, MOM, MOM_RSI] and the total subsections of all three of the sections. Each subsection contains TradingPair and Status. Is there a way I could do this with using a list comprehension preferably or a for loop without using a for data in [RSI, MOM, MOM_RSI]?

Code:

def reading(): 
    with open('data.json') as f:
        data = json.load(f)
    return data
reading()

JSON File:

{
    "RSI": [
      {
        "TradingPair": "BTCUSD",
        "Status": "ACTIVE",
      }
    ],
    "MOM":[
        {
            "TradingPair": "BCHUSDT",
            "Status": "PAUSED",
        },
        {
            "TradingPair": "ETHUSDT",
            "Status": "ACTIVE",
        }
    ],
    "MOM_RSI":[
        {
            "TradingPair": "BCHUSDT",
            "Status": "PAUSED",
        },
        {
            "TradingPair": "BTCUSDT",
            "Status": "ACTIVE",
        }
    ]
}
kaya3
  • 47,440
  • 4
  • 68
  • 97
tony selcuk
  • 709
  • 3
  • 11
  • Why do you keep asking different versions of this same question over and over again? This is the 3rd post of yours that I've seen that's almost exactly the same. Why not explain what the whole problem is with context, and then people can help you solve it rather than you splitting up the problem yourself but not offering any of your own code. Starting to think that we're solving the entire thing for some assignment, and the code you paste is just boilerplate. – blueteeth Jun 06 '21 at 23:05

2 Answers2

0

This will loop through the JSON file and get the number of subsections and list of items

the variable data must be the JSON file you have imported

subsections = 0 #stores the number of subsections
keys = [] #array to store list of items

for key in data:
    if isinstance(data[key], list): #check if the item is a list
        keys.append(key) #add the item to the list of items
    if len(data[key]) > 0: #check if the item has a length
        subsections += len(data[key]) #add the lenght/number of items to the subsections counter

sections = ','.join(keys) #creates a new string with the items in the keys array
print(sections)
print("Total subsections:", subsections)

Complete example with your code:

def reading(): 
    with open('data.json') as f:
        data = json.load(f)
        subsections = 0 #stores the number of subsections
        keys = [] #array to store list of items
       
        for key in data:
            if isinstance(data[key], list): #check if the item is a list
                keys.append(key) #add the item to the list of items
            if len(data[key]) > 0: #check if the item has a length
                subsections += len(data[key]) #add the lenght/number of items to the subsections counter
        
        sections = ','.join(keys) #creates a new string with the items in the keys array
        print(sections)
        print("Total subsections:", subsections)
reading()
FSchieber
  • 26
  • 1
  • 6
0

To get the first line of the output, simply use,

list(data)

as passing a dictionary as an iterable to the list constructor will iterate over its keys.

For the second line, use:

sum(len(subsections) for subsections in data.values())

I'm assuming you are able to convert this list of section headings, and the number of subsections, to your desired output string format.

If not, see

mkrieger1
  • 19,194
  • 5
  • 54
  • 65