Ask coding questions

← Back to all posts
Get JSON object from key
h
98129182 (8)

I’m trying to get a JSON object from key.
For example, the JSON object is something like this (I’m not actually using this, just an example):
{
"35": {
"num": 1,
"name": "Discovery",
"stream_type": "live",
"stream_id": "35"
},
"13085": {
"num": 2,
"name": "Discovery HD",
"stream_type": "live",
"stream_id": "13085"
},
"36": {
"num": 3,
"name": "Discovery Investigation",
"stream_type": "live",
"stream_id": "36"
},
"151": {
"num": 4,
"name": "Discovery Turbo",
"stream_type": "live",
"stream_id": "151"
}

}
How can I get, say, the object “151” with the name of “Discovery Turbo”?
I know I have asked this pretty badly, so if you don’t comprehend, tell me.
Thank you!
From yo boy @98129182
EDIT: I use Python.

Answered by Coder100 (17126) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (17126)

1. parse the json

import json

data = "..."

obj = json.loads(data)

https://www.makeuseof.com/tag/json-python-parsing-simple-guide

2. TREAT IT LIKE A DICT OWOOWWOWOOWOWOWOOWOWOWOWOWOOW

import json

data = "..."

obj = json.loads(data)
print(obj["151"])

idk man seems easy to me

98129182 (8)

@Coder100 Thank you! I’ll try.

98129182 (8)

@Coder100 I used an alternative method for this, but thank you for answering!

Coder100 (17126)

ah
what's the alternative? @98129182

98129182 (8)

@Coder100 Half-like yours. Just that I found a pattern for the JSON objects and used a [2] at the end. :)

Coder100 (17126)

erm
is this in a string or something

98129182 (8)

@SixBeeps ooo, how can I implement it? Sorry if I sound dumb.

SixBeeps (5067)

@98129182 jsonObject is the object from which you want to get the sub-object from. "151" is the key of that sub-object. So if you wanted to get Discovery HD, you'd call this:

jsonObject["13085"]