0

I'm trying to parse a JSON file and i couldn't yield exactly the results I'm expecting !

import json
import datetime
import sys
json_data=open('1.json')
server_details = json.load(json_data)
json_data.close()
for App_ID in server_details:
    type=[]
    x=0
    if "content-security-policy" in server_details[App_ID]:
        type.append("content-security-policy")
    if "x-frame-options" in server_details[App_ID]:
        type.append("x-frame-options")
    if "strict-transport-security" in server_details[App_ID]:
        type.append("strict-transport-security")
    if "x-content-type-options" in server_details[App_ID]:
        type.append("x-content-type-options")
    if "x-xss-protection" in server_details[App_ID]:
        type.append("x-xss-protection")
    headers_join=",".join(str(x) for x in type)
    header_complete=App_ID+","+headers_join
    for header in header_complete:
        print header,

When I try to print I get result as:

1 1 4 7 , 2 1 0 , 2 4 5 8 , s t r i c t - t r a n s p o r t - s e c u r i t y 1 7 7 1 , 1 8 3 4 , 2 4 8 7 , 3 , 1 1 5 9 , 1 3 1 7 , 1 1 , 1 0 9 6 , 1 3 1 3 , 1 3 1 2 , 2 6 5 , 4 2 2 , 1 5 5 6 , 2 4 9 7 , 2 0 6 8 , 2 0 6 1 , 2 2 5 2 , 2 2 5 0 , 1 0 8 3 , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 9 8 , 7 0 4 , 3 1 0 , x - f r a m e - o p t i o n s , x - c o n t e n t - t y p e - o p t i o n s 8 6 , 7 9 2 , 2 3 0 3 , 1 2 5 4 , 2 1 7 1 , 2 3 8 5 , x - f r a m e - o p t i o n s 2 3 8 4 , 9 1 5 , 2 0 7 2 , x - f r a m e - o p t i o n s 1 4 0 1 , 1 4 0 0 , 3 0 0 , 1 3 3 4 , 1 1 3 5 , 7 8 6 , 2 0 4 1 , 1 8 4 3 , 1 6 4 9 , 1 7 2 3 , 1 8 4 5 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 0 0 1 , 2 2 9 9 , 2 5 9 1 , 4 5 6 , 1 3 4 7 , 1 1 6 0 , 1 7 0 , s t r i c t - t r a n s p o r t - s e c u r i t y 2 5 6 9 , 9 7 6 , 7 3 0 , c o n t e n t - s e c u r i t y - p o l i c y , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 4 2 8 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y 1 5 0 2 , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s 2 1 1 9 , 1 5 0 0 , 1 8 7 , x - f r a m e - o p t i o n s 7 3 6 , s t r i c t - t r a n s p o r t - s e c u r i t y 5 0 2 , 4 6 9 , 2 5 8 8 , 3 5 5 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 6 8 , 1 9 1 7 , 2 2 1 3 , 4 7 0 , 2 2 1 1 , 1 0 2 6 , 1 7 , 2 2 3 6 , 1 0 2 9 , x - f r a m e - o p t i o n s , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 1 6 5 , 8 8 7 , 2 6 3 8 , 2 4 3 3 , 5 5 6 , 2 6 3 2 , 2 6 2 6 , 2 0 8 5 , 1 6 1 7 , 2 3 4 8 , 1 4 4 7 , 1 1 1 5 , 2 5 0 8 , 1 2 9 2 , 3 2 8 , 6 1 0 , 7 2 , 1 0 4 1 , 1 9 7 7 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y 2 1 2 8 , x - f r a m e - o p t i o n s 1 6 0 6 , 1 6 8 1 , x - f r a m e - o p t i o n s 1 3 6 4 , 3 5 3 ,

Exact Results has to be like:

1147,
210,
2458,strict-transport-security
1771,
1834,
2487,
3,
1159,
1317,
11,
1096,
1313,
1312,
265,
422,
1556,
2497,
2068,
2061,
2252,
2250,
1083,strict-transport-security,x-content-type-options,x-xss-protection
198,
704,
310,x-frame-options,x-content-type-options
86,
792,
2303,
1254,
2171,
2385,x-frame-options
2384,
915,
2072,x-frame-options
1401,
1400,
300,
1334,
1135,
786,
2041,
1843,
1649,
4
  • Shouldn't there be a A p p _ I D , in the start of that output? Commented May 27, 2015 at 7:50
  • @hyades: no, that's not a string literal, it is another variable. Commented May 27, 2015 at 7:50
  • @MartijnPieters ok whatever value that variable holds and also the comma should have been in the output Commented May 27, 2015 at 7:52
  • @hyades: yes, and that means the value was 1147. The next line it is 210, etc. Note that the loop only adds strings like content-security-policy and x-xss-protection, not digits. Commented May 27, 2015 at 7:54

1 Answer 1

4

header_complete is a single string:

headers_join=",".join(str(x) for x in type)
header_complete=App_ID+","+headers_join

The ",".join() expression produces a string, and you then pre-pend the app ID in front of that.

You then loop over that string to produce each character, printed separately, with a comma to tell print to omit the newline:

for header in header_complete:
    print header,

Just remove the loop; just use:

print header_complete

You are really just filtering out certain options from the per-AppId list. Rather than a series of if statements, you could use a list comprehension:

for appid, headers in server_details.iteritems():
    htypes = [h for h in headers if h in (
                  'content-security-policy', 'x-frame-options',
                  'strict-transport-security', 'x-content-type-options',
                  'x-xss-protection')]
    print '{},{}'.format(App_ID, ','.join(htypes))
Sign up to request clarification or add additional context in comments.

4 Comments

Many Thanks for reaching out Mart ! When I'm trying to out-write with open('Output.txt', 'wb') as f: f.write(header_complete+'\n') I almost got nothing except only one ID in the file "353," !
@ArunPrabhuD: you are only writing the last line to your file. Open the file once outside the loop, and use the f.write() call in your loop.
If so ,how to write the entire "print header_complete" results to a text file ?
@ArunPrabhuD: you are already doing that. You have one such line for each app id. So outside the loop, open the file, inside the loop, instead of print use f.write().

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.