RinW
RinW

Reputation: 553

Python: How to set a value from JSON as the index in a list?

I'm trying to read a dataset and set the integer value of the JSON file as the array of the list. This is the example JSON file,

[{
"index_id": "1234",
"text": "hello world",
},
{
"index_id": "5678",
"text": "roses are red",
}]

Right now, I have just tried with reading the JSON file and putting everything to a defaultdict(list), this messes things up. Assume I read everything to L1

If I try to get L1[1234] this would give an error as 1234 is not a valid index in the L1 and the indexes are 0,1.

If L1 was printed,

{u'1234': u'hello world'}, {u'5678': u'roses are red'}]

I understand that the list has my potential value for the index as a value stored and in unicode (makes it worse).

So how to turn L1 into or a method so if I try to pull up L1[1234] it would pull up the 'hello world',

{1234: u'hello world'}, {5678: u'roses are red'}]

Thank you

Edited: Changed the JSON.

Upvotes: 0

Views: 2603

Answers (5)

Jyotsna
Jyotsna

Reputation: 336

In case you are reading from a json file, when json is loaded the type of data is dictionary and you can directly read the keys of loaded data. Still if you want to create a list out of it, please refer below code

My sample.json file

{ "1234": { "id": "blabla", "iscategorical": "0" }, "5678": { "id": "valore" }, "8975": "value", "6985": { "id": "valore" } }

Code in separate python file:

import json

import io

from collections import defaultdict

with io.open('sample.json') as data_file:

data_loaded = json.load(data_file)

print(data_loaded)

print(type(data_loaded))

l1 = defaultdict(list)

for key in data_loaded:

l1[key] = data_loaded[key]

print(l1)

print(l1['1234'])

Upvotes: 0

ewwink
ewwink

Reputation: 19154

Change your json like this

L1 = {
    "1234": "hello world",
    "5678": "roses are red"
}

# call it with quote or as string

print L1["1234"]

or create function

jsonList = [{
    "1234": "hello world"
}, 
{
    "5678": "roses are red"
}]


def L1(key):
  key = str(key)
  for i in jsonList:
    if key in i:
      return i[key]

print L1(5678)

Upvotes: 0

bebopspider
bebopspider

Reputation: 1

I think you can read this in as a python dictionary, where 1234 and 5678 are "keys" and the respective strings are the values. For example,

{
  1234: 'hello world', 
  5678: 'roses are red'
}

You can index into it as you have mentioned, L1[1234] and you will get 'hello world'. You can read a bit about dictionaries here.

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71570

Or try merging list of dictionaries:

>>> [i['1234'] for i in L1 if '1234' in i][0]
'hello world'
>>> 

Whole thing:

>>> L1=[{
    "1234": "hello world"
}, 
{
    "5678": "roses are red"
}]
>>> [i['1234'] for i in L1 if '1234' in i][0]
'hello world'
>>> 

Upvotes: 0

Dani Mesejo
Dani Mesejo

Reputation: 61910

Assuming you have a list of dicts you could do something like this:

json_lst = [{
    "1234": "hello world"
}, 
{
    "5678": "roses are red"
}]

result = {int(k) : v  for element in json_lst for k, v in element.items()}
print(result[1234])

Output

hello world

The above dictionary comprehension is equivalent to the following nested loops:

result = {}
for element in json_lst:
    for k, v in element.items():
          result[int(k)] = v 

Upvotes: 1

Related Questions