1

Here is a sample of my Json file :

{ 'data':[{'word':'first_word',
           'vector':[[0.4][0.2][0.8] } ,
          {'word':'second_word',
           'vector':[[0.2][0.65][0.7] }
          ]}

I want to access to values of a given word and its vector, and store it in a variable. Here's what I did :

with open('./Vectors.json') as json_file:
    data_dict = json.loads(json_file)
    for word in words: 
        vector = data_dict["Data"][0][w]["vector"]

It returns to me the following error :

TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper
2
  • 1
    Try json.load instead of json.loads Commented Jun 23, 2020 at 17:55
  • @JustinEzequiel this keeps running without giving me anything Commented Jun 23, 2020 at 18:26

1 Answer 1

1

you have to use json.load,

json.loads it is used when your json is in a string, bytes or bytearray

with open('./Vectors.json') as json_file:
    data_dict = json.load(json_file)

you can use a dict to map for each word the vector values using a dictionary comprehension:

word_vect = {d['word']: d['vector'] for d in data_dict['data']}
Sign up to request clarification or add additional context in comments.

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.