0

I'm trying to sort this json by 'Count' key values:

{
  "17-April": {
    "https://www.dom1.com/": [
      {
        "Cant": 5
      },
      {
        "Links": [
          "https://www.dom1.com/1",
          "https://www.dom1.com/2",
          "https://www.dom1.com/3",
          "https://www.dom1.com/4",
          "https://www.dom1.com/5"
        ]
      }
    ],
    "https://www.dom2.com/": [
      {
        "Cant": 3
      },
      {
        "Links": [
          "https://www.dom2.com/",
          "https://www.dom2.com/",
          "https://www.dom2.com/"
    ],
    "https://www.dom3.com/": [
      {
        "Cant": 4
      },
      {
        "Links": [
          "https://www.dom3.com/1",
          "https://www.dom3.com/2",
          "https://www.dom3.com/3",
          "https://wwwdom3.com/4"
        ]
      }
    ]
  },
  "19-April": {
    "https://www.dom1.com/": [
      {
        "Cant": 7
      },
      {
        "Links": [
          "https://www.dom1.com/1",
          "https://www.dom1.com/2",
          "https://www.dom1.com/3",
          "https://www.dom1.com/4",
          "https://www.dom1.com/5",
          "https://www.dom1.com/6",
          "https://www.dom1.com/7"
        ]
      }
    ],
    "https://www.dom2.com/": [
      {
        "Cant": 2
      },
      {
        "Links": [
          "https://www.dom2.com/",
          "https://www.dom2.com/"
        ]
      }
    ],
    "https://www.dom3.com/": [
      {
        "Cant": 8
      },
      {
        "Links": [
          "https://www.dom3.com/1",
          "https://www.dom3.com/2",
          "https://www.dom3.com/3",
          "https://www.dom3.com/4",
          "https://www.dom3.com/5",
          "https://www.dom3.com/6",
          "https://www.dom3.com/7",
          "https://www.dom3.com/7"
        ]
      }
    ]
  }
}
}

This json gets repeated by date, I would like to sort it by the values of the "Count" key.

This json takes several domains by date, thats why I want to sorted by count, To have it nicely sorted by domain wit more links every day

The ideal sorting it would be: by date -> count

13
  • 1
    When you say This json gets repeated by date what do you mean. Is there a list holding this that you didn't show? You've only shown one dictionary. It doesn't make sense to sort that. Commented Apr 18, 2020 at 3:01
  • @MarkMeyer Sorry if I didn't put many details. I have a script that I execute every day and opens this json file and adds the same structured data only changing date key and key values of count and links. Commented Apr 18, 2020 at 3:18
  • Does domain have multiple entries? Commented Apr 18, 2020 at 3:18
  • This json takes several domains by date, thats why I want to sorted by count, To have it nicely sorted by domain wit more links every day Commented Apr 18, 2020 at 3:19
  • @JamesPowis Yes Commented Apr 18, 2020 at 3:20

1 Answer 1

2

So the data is in rough shape... It is a dictionary with dates in a non-sortable format, I would recommend YYYY-MM-DD so that it sorts cleanly. The values of the dates are dictionaries with anonymous keys whos value is an array dissimilar items.

I would highly recommend storing the data like the output of this: [{"date":"YYYY-MM-DD", "domains": [{"domain": "string:", "count": 1, "links": ["link1"]}]}]

If you cannot do that, then this chunk will migrate your example into that structure:

cleaned_data = []
for date, v in data.items():
    o = {
        'date': date,
        'domains': []
    }
    for domain, dv in v.items():
        o['domains'].append({
            'domain': domain,
            'count': dv[0]['Cant'],
            'links': dv[1]['Links']
        })
    cleaned_data.append(o)

And this chunk will sort it as desired. (note I am not playing with datetime conversions... I don't consider that inscope for this question.)

for v in cleaned_data:
    v['domains'] = sorted(v['domains'], key = lambda i: i['count'])

sorted_data = sorted(cleaned_data, key = lambda i: i['date'])
print(sorted_data)
Sign up to request clarification or add additional context in comments.

3 Comments

If you look at the edit, you'll see that "Domain" isn't actually a key (unfortunately).
You get a vote from me for working through that json format! Nice suggestion for new data format too.
@JamesPowis Thank you so much! It worked. I changed the json structured and worked perfectly.

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.