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

cullzie
  • 2,705
  • 2
  • 16
  • 21
itgeek
  • 549
  • 1
  • 15
  • 33

1 Answers1

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.

razdi
  • 1,388
  • 15
  • 21
  • 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. – itgeek Apr 26 '19 at 17:37
  • 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 – itgeek Apr 26 '19 at 17:45
  • 1
    Following the answer on https://stackoverflow.com/questions/47659782/python-how-convert-single-quotes-to-double-quotes-to-format-as-json-string?rq=1 post, you can do the following: `f = open('strings.json', 'r') s = f.read().strip() l = eval(s) json.dumps(l)` – razdi Apr 28 '19 at 23:15
  • 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 – itgeek May 03 '19 at 04:26
  • 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 – razdi May 04 '19 at 06:05