Repository: pandas-dev/pandas

Issue:
pd.read_json and orient="index" sorts results
Note that the order of input is not maintained with orient="index"

```python
>>> df = pd.DataFrame(range(3), index=range(3, 0, -1))
>>> df
   0
3  0
2  1
1  2
>>> df.to_json(orient="index")
'{"3":{"0":0},"2":{"0":1},"1":{"0":2}}'
>>> pd.read_json(df.to_json(orient="index"), orient="index")
   0
1  2
2  1
3  0
```

This is not necessarily a problem as by definition JSON objects are unordered, but preserving the order here would be a logical follow up to #27309


Discussion:
Hi !
If this issue is not assigned, I would like to make PR for this :)

This is the result you expected, right?
```python
...
>>> pd.read_json(df.to_json(orient="index"), orient="index")
   0
3  0
2  1
1  2
```
Yes that's correct, at least for Py36 and above. The lines where this is happening is here:

https://github.com/pandas-dev/pandas/blob/ffe6cfdbf82d663c3f77567bde11f1666de1df38/pandas/io/json/_json.py#L1114

So for now look in pandas.compat for the PY35 directive and only sort for that. Py36 and above won't need extra handling.

We'll eventually be ripping out Py35 by the time this gets released but good to do it that way so the subsequent clean up is easily recognizable.
I have completed the code modification to resolve the issue you mentioned. However, an error occurs because the existing test file is checking the sorting at Py36 and above.
https://github.com/pandas-dev/pandas/blob/4fb853f62e5dd1b42a8054e930bd9cacbe9baa0f/pandas/tests/io/json/test_pandas.py#L171-L173

Therefore, can I modify the test file to solve the errors, and then move on to PR?
Yep the text will need to change to only sort on Py35

Resolving patch:
diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py
--- a/pandas/io/json/_json.py
+++ b/pandas/io/json/_json.py
@@ -12,7 +12,7 @@
 
 from pandas.core.dtypes.common import ensure_str, is_period_dtype
 
-from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
+from pandas import DataFrame, MultiIndex, Series, compat, isna, to_datetime
 from pandas._typing import Scalar
 from pandas.core.reshape.concat import concat
 
@@ -1112,15 +1112,13 @@ def _parse_no_numpy(self):
             self.check_keys_split(decoded)
             self.obj = DataFrame(dtype=None, **decoded)
         elif orient == "index":
-            self.obj = (
-                DataFrame.from_dict(
-                    loads(json, precise_float=self.precise_float),
-                    dtype=None,
-                    orient="index",
-                )
-                .sort_index(axis="columns")
-                .sort_index(axis="index")
+            self.obj = DataFrame.from_dict(
+                loads(json, precise_float=self.precise_float),
+                dtype=None,
+                orient="index",
             )
+            if compat.PY35:
+                self.obj = self.obj.sort_index(axis="columns").sort_index(axis="index")
         elif orient == "table":
             self.obj = parse_table_schema(json, precise_float=self.precise_float)
         else:


Test patch:
diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py
--- a/pandas/tests/io/json/test_pandas.py
+++ b/pandas/tests/io/json/test_pandas.py
@@ -166,8 +166,7 @@ def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype):
 
         expected = self.frame.copy()
 
-        if not numpy and (orient == "index" or (PY35 and orient == "columns")):
-            # TODO: debug why sort is required
+        if not numpy and PY35 and orient in ("index", "columns"):
             expected = expected.sort_index()
 
         assert_json_roundtrip_equal(result, expected, orient)
@@ -181,7 +180,7 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
             data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype
         )
         expected = self.intframe.copy()
-        if not numpy and (orient == "index" or (PY35 and orient == "columns")):
+        if not numpy and PY35 and orient in ("index", "columns"):
             expected = expected.sort_index()
 
         if (
@@ -216,7 +215,7 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
         )
 
         expected = df.copy()
-        if not numpy and (orient == "index" or (PY35 and orient == "columns")):
+        if not numpy and PY35 and orient in ("index", "columns"):
             expected = expected.sort_index()
 
         if not dtype: