JSON objects, like Python dictionaries, have no order. You can also not control how much of an object is loaded, not with the standard library json module at any rate.
After loading, you could take the ten key-value pairs with the lowest key value:
import heapq
import json
data = json.loads(json_string)
limited = {k: data[k] for k in heapq.nsmallest(data, 10, key=int)}
The heapq.nsmallest() will efficiently pick out the 10 smallest keys regardless of the size of data.
Of course, if the keys are always consecutive and always start at 1, you may as well use a range() here:
data = json.loads(json_string)
limited = {str(k): data[str(k)] for k in range(1, 11)}
If you want to capture the objects in file definition order you could use the object_pairs_hook argument to json.load() and json.loads():
class FirstTenDict(dict):
def __init__(self, pairs):
super(FirstTenDict, self).__init__(pairs[:10])
data = json.loads(json_string, object_pairs_hook=FirstTenDict)
Demo of the latter approach:
>>> import json
>>> class FirstTenDict(dict):
... def __init__(self, pairs):
... super(FirstTenDict, self).__init__(pairs[:10])
...
>>> json_data = '''\
... {"foo42": "bar", "foo31": "baz", "foo10": "spam", "foo44": "ham", "foo1": "eggs",
... "foo24": "vikings", "foo21": "monty", "foo88": "python", "foo11": "eric", "foo65": "idle",
... "foo13": "will", "foo31": "be", "foo76": "ignored"}
... '''
>>> json.loads(json_data)
{'foo1': 'eggs', 'foo88': 'python', 'foo44': 'ham', 'foo10': 'spam', 'foo76': 'ignored', 'foo42': 'bar', 'foo24': 'vikings', 'foo11': 'eric', 'foo31': 'be', 'foo13': 'will', 'foo21': 'monty', 'foo65': 'idle'}
>>> json.loads(json_data, object_pairs_hook=FirstTenDict)
{'foo1': 'eggs', 'foo88': 'python', 'foo44': 'ham', 'foo10': 'spam', 'foo24': 'vikings', 'foo11': 'eric', 'foo21': 'monty', 'foo42': 'bar', 'foo31': 'baz', 'foo65': 'idle'}
>>> import pprint
>>> pprint.pprint(_)
{'foo1': 'eggs',
'foo10': 'spam',
'foo11': 'eric',
'foo21': 'monty',
'foo24': 'vikings',
'foo31': 'baz',
'foo42': 'bar',
'foo44': 'ham',
'foo65': 'idle',
'foo88': 'python'}
.get()methodjson.dumphas asort_keysparameter is because sometimes people do care about the order of keys in the file. It's just highly unusual for this caring to extend so far as loading the file, usually it's only for human-readability.