1

I have List of multiple dictionaries inside it(as JSON ).I have a list of value and based on that value I want that JSON object for that particular value. For eg.

[{'content_type': 'Press Release',
  'content_id': '1',
   'Author':John},
{'content_type': 'editorial',
  'content_id': '2',
   'Author': Harry
},
{'content_type': 'Article',
  'content_id': '3',
   'Author':Paul}]

I want to to fetch complete object where author is Paul. This is the code I have made so far.

import json
newJson = "testJsonNewInput.json"
ListForNewJson = []
def testComparision(newJson,oldJson):
   with open(newJson, mode = 'r') as fp_n:
    json_data_new = json.load(fp_n) 
for jData_new in json_data_new:
    ListForNewJson.append(jData_new['author'])

If any other information required, please ask.

6
  • Are you going to be performing these sorts of searches more than once? Commented Nov 23, 2017 at 7:10
  • Yes. There can be multiple authors I want to find and the loop will make it a bit slow. Commented Nov 23, 2017 at 7:12
  • Possible duplicate of Get key by value in dictionary Commented Nov 23, 2017 at 7:20
  • stackoverflow.com/questions/40827356/… Commented Nov 23, 2017 at 7:21
  • @LaxmikantGurnalkar Maybe, but there are other hidden nuances to OP's question, which I feel my answer better addresses (that the target won't). Commented Nov 23, 2017 at 7:26

2 Answers 2

1

Case 1
One time access

It is perfectly alright to read your data and iterate over it, returning the first match found.

def access(f, author):
    with open(file) as f:
        data = json.load(f)

    for d in data:
        if d['Author'] == author:
            return d
    else:
        return 'Not Found'

Case 2
Repeated access

In this instance, it would be wise to reshape your data in such a way that accessing objects by author names is much faster (think dictionaries!).

For example, one possible option would be:

with open(file) as f:
    data = json.load(f)

newData = {}
for d in data:
    newData[d['Author']] = d

Now, define a function and pass your pre-loaded data along with a list of author names.

def access(myData, author_list):
    for a in author_list:
        yield myData.get(a)

The function is called like this:

for i in access(newData, ['Paul', 'John', ...]):
    print(i)

Alternatively, store the results in a list r. The list(...) is necessary, because yield returns a generator object which you must exhaust by iterating over.

r = list(access(newData, [...]))
Sign up to request clarification or add additional context in comments.

Comments

0

Why not do something like this? It should be fast and you will not have to load the authors that wont be searched.

alreadyknown = {}
list_of_obj = [{'content_type': 'Press Release',
    'content_id': '1',
    'Author':'John'},
    {'content_type': 'editorial',
    'content_id': '2',
    'Author': 'Harry'
    },
    {'content_type': 'Article',
    'content_id': '3',
    'Author':'Paul'}]
def func(author):
    if author not in alreadyknown:
        obj = get_obj(author)
        alreadyknown[author] = obj
    return alreadyknown[author]
def get_obj(auth):
    return [obj for obj in list_of_obj if obj['Author'] is auth]
print(func('Paul'))

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.