I have the following json data:
[
{
"date_created": "2019-12-25 12:57:58",
"date_updated": "2020-12-18 16:18:29",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 2,
"id": 1,
"image_path": "",
"on_discount": false,
"price": 20,
"title": "The Art of Scala"
},
{
"date_created": "2019-03-16 05:15:39",
"date_updated": "2020-12-29 08:40:44",
"description": "Agile practices discover requirements and develop solutions through collaborative effort.",
"discount_price": 3,
"id": 2,
"image_path": "",
"on_discount": true,
"price": 30,
"title": "The Joy of Agile"
},
{
"date_created": "2020-09-13 14:40:39",
"date_updated": "2020-09-23 10:52:39",
"description": "Pages is a word processor developed by Apple Inc.",
"discount_price": 5,
"id": 3,
"image_path": "",
"on_discount": false,
"price": 50,
"title": "Talks About Pages"
},
{
"date_created": "2020-07-20 17:51:23",
"date_updated": "2020-08-04 12:06:58",
"description": "Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft.",
"discount_price": 2,
"id": 4,
"image_path": "",
"on_discount": false,
"price": 20,
"title": "This Is A Course About Visual Studio"
},
{
"date_created": "2020-07-04 01:02:49",
"date_updated": "2021-01-31 15:07:20",
"description": "Scala is a multi-paradigm, general-purpose programming language.",
"discount_price": 3,
"id": 5,
"image_path": "",
"on_discount": true,
"price": 30,
"title": "Even A Kid Can Learn Scala!"
}]
I want to fetch a record using the key 'id' from the data. I did in the following way:
for i in data_id:#data_id is the json objects
if i['id']==id:
print(i)
How can I do this without linear scan. As if there is large amount of data, it takes huge amount of time for the last variable. So, I want the access time for all the variables to be same
Why not use Pandas?
df = pd.DataFrame(your_json_data)
display(df)
Prints out:
date_created date_updated description discount_price id image_path on_discount price title
0 2019-12-25 12:57:58 2020-12-18 16:18:29 Scala is a multi-paradigm, general-purpose pro... 2 1 False 20 The Art of Scala
1 2019-03-16 05:15:39 2020-12-29 08:40:44 Agile practices discover requirements and deve... 3 2 True 30 The Joy of Agile
2 2020-09-13 14:40:39 2020-09-23 10:52:39 Pages is a word processor developed by Apple Inc. 5 3 False 50 Talks About Pages
3 2020-07-20 17:51:23 2020-08-04 12:06:58 Microsoft Visual Studio is an integrated devel... 2 4 False 20 This Is A Course About Visual Studio
4 2020-07-04 01:02:49 2021-01-31 15:07:20 Scala is a multi-paradigm, general-purpose pro... 3 5 True 30 Even A Kid Can Learn Scala!
Now you simply can select by id
with df[df.id =="id"]
.