0

I want a conditional loop which should read json file, and print "postId" based on "user_name" matches an item in the list.

I tried these baby steps to see, but it did not work

Python version: 2.7.5 OS: Linux

here's python script:

import sys
import re
import json

my_list = ['windows-super-user','linux user','unix_super_user']

for list_item in my_list:
    with open('strings.json') as f:
        d = json.load(f)
        for userid in d:
            if list_item ==  user_name: 
                print("Here's the user name :" +user_name+ ", and post id :" +postid)

Here's the strings.json file content;

[
    {
        "postId":"328e9497740b456154c636349",
        "postTimestamp": "1521543600",
        "pageType": "/home.php:topnews",
        "viewTime": 1521545993647,
        "user_name": "windows-super-user",
        "gender": 3,
        "likes": "8",
        "id": "ffa1e07529ac917f6d573a",
        "postImg": 1,
        "postDesc": [753],
        "origLink": 0,
        "duration": 0,
        "timestamp": 9936471521545,
        "back_time": 1521545993693
    },
    {
        "postId":"15545154c636349",
        "postTimestamp": "547773600",
        "pageType": "/home.php:topnews",
        "viewTime": 45993647,
        "user_name": "linux user",
        "gender": 3,
        "likes": "8",
        "id": "695e45a17f6d573a",
        "postImg": 1,
        "postDesc": [953],
        "origLink": 0,
        "duration": 0,
        "timestamp": 545993647,
        "back_time": 85993693
    },
    {
        "postId":"9098897740b456154c636349",
        "postTimestamp": "899943600",
        "pageType": "/home.php:topnews",
        "viewTime": 1521545993647,
        "user_name": "unix_super_user",
        "gender": 3,
        "likes": "8",
        "id": "917f6d573a695e45affa1e07",
        "postImg": 1,
        "postDesc": [253],
        "origLink": 0,
        "duration": 0,
        "timestamp": 193647,
        "back_time": 1521545993693
    },

]

Expected output:

Here's the user name : windows-super-user , and post id : 328e9497740b456154c636349
Here's the user name : linux user , and post id : 15545154c636349
Here's the user name : unix_super_user , and post id : 9098897740b456154c636349

0

1 Answer 1

2

Try iterating like this:

import sys
import re
import json

my_list = ['windows-super-user','linux user','unix_super_user']

with open('strings.json') as f:
  d = json.load(f)
  for elem in d:
    if elem["user_name"] in my_list:
      print("Here's the user name :" +elem["user_name"]+ ", and post id :" +elem["postId"])

I open the file and load the json, just as you did. But I'm only opening the file once and checking for a match with the user_name. If a match is found, I print out the name and the post id.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks karan. Do you know if there any simple way to convert all single quotes to double quotes in a json file. e.g. the above mentioned json file strings.json has all single quotes in my case.
I have tried below code; with open('strings.json') as f: data = json.dumps(f) print(data) error: TypeError: Object of type 'TextIOWrapper' is not JSON serializable^M
Following the answer on stackoverflow.com/questions/47659782/… post, you can do the following: f = open('strings.json', 'r') s = f.read().strip() l = eval(s) json.dumps(l)
it prints only one value Here's the user name : windows-super-user , and post id : 328e9497740b456154c636349 INSTEAD OF ALL THREE THREE STATEMENTS Here's the user name : windows-super-user , and post id : 328e9497740b456154c636349 Here's the user name : linux user , and post id : 15545154c636349 Here's the user name : unix_super_user , and post id : 9098897740b456154c636349
I checked the code and ran with the json you've provided, it gives me all three outputs. Have you changed something perhaps? If you could update the code you're using I could try to help you out

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.