skip to Main Content

I have online JSON file where I have something like this:

{'skladiste': 1, 'sifra': '7138', 'nc': 0.8, 'vpc': 47.01, 'mpc': 55.0, 'stanje': 5.0, 'aktivan': 255, 'lokacija': '', 'atraktivanod': 0, 'atraktivando': 0}
{'skladiste': 2, 'sifra': '7138', 'nc': 0.8, 'vpc': 47.01, 'mpc': 55.0, 'stanje': 2.0, 'aktivan': 255, 'lokacija': '', 'atraktivanod': 0, 'atraktivando': 0}

Now I need to get that JSON data, sum ‘stanje’ for same key which is ‘sifra’ through whole JSON file. Is there any help?

I got JSON file data with this but I can not sum data

from urllib.request import urlopen
  
# import json
import json
# store the URL in url as 
# parameter for urlopen
url = "https://www6.eline.ba/bl/RestWebShop.svc/json/CjenovniciZaWeb/ee6e994652884578830402a297ef3a93/tobyshop"
  
# store the response of URL
response = urlopen(url)
  
# storing the JSON response 
# from url in data
data_json = json.loads(response.read())

# print the json response
print(data_json)

For example, there is ‘sifra’ 7138 repeated 2 times in this data with ‘stanje’ 5 and 2 and now I need to sum this two and export new data with only one ID 7138 and stanje with data of 7.

2

Answers


  1. As I understand after reading your comments, your goal is to have the same json as output, just grouping those elements that have the same ‘sifra’.

    These are the steps needed:

    First step is to get the list of items from the json, that is inside the key ‘cjenovnici’:

    original_list = data_json['cjenovnici']
    

    Then you sort the list using ‘sifra’:

    from itertools import groupby
    from operator import itemgetter
    
    sorted_list = sorted(my_list, key = itemgetter('sifra'))
    

    And once the list is sorted you can use groupby to create a new list:

    grouped_list = []
    for key, value in groupby(sorted_list, itemgetter('sifra')):
      list_value = list(value)
      element = list_value[0]
      if len(list_value) >= 1:
        # if there is more than 1 element with the same 'sifra', sum 'stranje' of them
        element['stanje'] = sum(item['stanje'] for item in list_value)
      grouped_list.append(element)  
    

    Now you can replace the original list with the grouped list in data_json:

    data_json['cjenovnici'] = grouped_list
    

    And as final step you can save the data as a new json file:

    with open('json_data.json', 'w') as outfile:
        json.dump(data_json, outfile)
    
    Login or Signup to reply.
  2. You can iterate the json the sum them.

    Code:

    output_dict = {}
    dict_list = [{'skladiste': 1, 'sifra': '7138', 'nc': 0.8, 'vpc': 47.01, 'mpc': 55.0, 'stanje': 5.0, 'aktivan': 255, 'lokacija': '', 'atraktivanod': 0, 'atraktivando': 0},
    {'skladiste': 2, 'sifra': '7138', 'nc': 0.8, 'vpc': 47.01, 'mpc': 55.0, 'stanje': 2.0, 'aktivan': 255, 'lokacija': '', 'atraktivanod': 0, 'atraktivando': 0}]
    
    for row in dict_list:
        if row.get("sifra"):
            output_dict[row.get("sifra")] = output_dict.get(row.get("sifra"),0) + row.get("stanje", 0)
            
    output_dict
    

    Output:

    {'7138': 7.0}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search