Change language

Python | Sort JSON by value

What is JSON?
JSON (JavaScript Object Notation) — it is a lightweight text-based, language-independent data exchange format that is easy to read and write for humans and machines. JSON can represent two structured types: objects and arrays. Object — it is an unordered collection of zero or more name / value pairs. The array — it is an ordered sequence of zero or more values. Values ​​can be strings, numbers, booleans, zero, and these two structured types.

The challenge is to sort the JSON first by code , then by class followed by enrollment_no .

Code # 1: sort in order

# Python code to demonstrate JSON sorting.

import json

 

data = & # 39; & # 39; & # 39; {

& quot; Student & quot;: [

{

& quot; Enrollment_no & quot ;: & quot; 9915103000 & quot ;,

& quot; Title & quot ;: & quot; JIIT & quot ;,

& quot; subject & quot;: [

{

  "Code": "DBMS",

& quot; Class & quot ;: & quot; C & quot;

  }

]

< code class = "comments">},

{

& quot; Enrollment_no & quot ;: & quot; 8815103057 & quot ;,

  & quot; Title & quot ;: & quot; JSS & quot ;,

  & quot; subject & quot;: [

{

& quot; Code & quot ;: & quot; COA & quot ;,

& quot; excellent grade & quot;

},

{

& quot; Code & quot ;: & quot; CN & quot ;,

 

& quot; Class & quot ;: & quot; A + & quot;

}

]

}

  ]

} & # 39; & # 39;"

 
# Parse the Json object

json_parse = json.loads (data)

 
# repeat

for it in json_parse [ ’Student’ ]:

< code class = "undefined spaces">  for y in it [ ’subject’ ]:

print (y [ ’code’ ], y [ ’ grade’ ], it [ ’enrollment_no’ ], it [ ’ name’ ])

Output:

 DBMS C 9915103000 JIIT COA A 8815103057 JSS CN A + 8815103057 JSS 

Code # 2: using an external library like like Pandas (sort in ascending order).

 from pandas.io.json import json_normalize

 

df = json_normalize (json_parse [ ’Student’ ],

’subject’

[ ’enrollment_no’ , ’name’ ])

  

df.sort_values ​​([ ’code’ , ’grade’ , ’ enrollment_no’ ]). .reset_index (drop = True )

Exit :

 code grade enrollment_no name 0 CN A + 8815103057 JSS 1 COA A 8815103057 JSS 2 DBMS C 9915103000 JIIT 

Shop

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically