0

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.

3
  • This is not json. this is list of two different dicts/json Commented Oct 30, 2018 at 3:07
  • I edited the question now. Commented Oct 30, 2018 at 11:56
  • Instead if you could show sample of dataset. Commented Oct 30, 2018 at 13:50

5 Answers 5

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

This seemed to be the solution which worked with minimal changes to the original datafile. Thanks for this.
0

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'
>>> 

1 Comment

@DanielMesejo Now?
0

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.

1 Comment

But how do you get that dict?
0

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)

Comments

0

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.