Extract data from JSONs inside the CSV [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Could you advise what would be the best way to extract an element from JSON when I have multiple JSONs in a CSV file?
The data structure is like this
Line1: {"CreationTime":"2018-10-29T13:40:13","Id":"35127aae-a888-4665-6514-08d63da40e14","Operation":....,"IPaddress":"x.x.x.x"...}
Line 2: {"CreationTime":"2018-10-29T13:41:13","Id":"35127aae-a888-4665-6514-08d63da40e14","Operation":....,"IPAddress":"x.x.x.x"...}
.....
LineYY:...
My goal is to extract the IPAddress value from every line, that means from every json array. What would be the best way to do it?
Thanks a lot!

You can use json to load each line into a dictionary and then access IPAddress
import json
ips = []
for line in data.readlines():
row = json.loads(line)
ips.append(row['IPAddress'])

Related

How to get a specific string from file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a JSON file like this:
{"objects":[{"featureId":"ckm39acfw00043b6a4i8vv8zf","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{
top":110,"left":799,"height":42,"width":53},"instanceURI":<"https://api.labelbox.com/masks/feature/ckm39acfw00043b6a4i8vv8zf?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39agzr00073b6alzzwpm77","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":
{"top":151,"left":875,"height":45,"width":120},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39agzr00073b6alzzwpm77?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39an0e000a3b6ae7vc0bo8","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":635,"left":952,"height":93,"width":84},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39an0e000a3b6ae7vc0bo8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39bbki000g3b6au6s5s3se","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":646,"left":764,"height":74,"width":93},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39bbki000g3b6au6s5s3se?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39cgdi000p3b6aru669fzh","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":
{"top":375,"left":916,"height":52,"width":80},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cgdi000p3b6aru669fzh?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39ckyi000s3b6armui3tn3","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":420,"left":914,"height":72,"width":86},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39ckyi000s3b6armui3tn3?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"
},{"featureId":"ckm39cp6a000v3b6a6cjj16xp","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":478,"left":867,"height":66,"width":137},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cp6a000v3b6a6cjj16xp?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39cyom000y3b6aqp2x5i0s","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":703,"left":806,"height":85,"width":95},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39cyom000y3b6aqp2x5i0s?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39dz3t00143b6a2brbj4qi","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":41,"left":823,"height":50,"width":80},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39dz3t00143b6a2brbj4qi?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"},{"featureId":"ckm39eco400173b6a35p84q7y","schemaId":"ckm399dnn07ax0y8hdncv51yy","title":"Buildings","value":"buildings","color":"#1CE6FF","bbox":{"top":31,"left":892,"height":62,"width":95},"instanceURI":"https://api.labelbox.com/masks/feature/ckm39eco400173b6a35p84q7y?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja20yN3F1aDVwNzh2MDc4OXh3YmE3eWo5Iiwib3JnYW5pemF0aW9uSWQiOiJja20yN3F0cm1wNzhvMDc4OXBlaHJiZG4wIiwiaWF0IjoxNjE1MzcyNjUxLCJleHAiOjE2MTc5NjQ2NTF9.ALYeG0mpNvnOpAuj6O3h0OFcrREOtOvJqqVqqt8xcqw"}],"classifications":[]}
and I want to save in array all occurrences of top, left, right, height and width. How can I do that?
Firstly, the JSON file does not look valid. For my answer I'll assume a valid file like so:
#example.json
{
"top":151,
"left":875,
"height":45,
"width":120
}
Next you can use python's builtin library 'json' to load the json and extract information:
import json
with open('test.json', 'r') as fh:
d = json.load(fh)
print(d.get('top'))
Notice that first you load the json file's content into a dictionary and then you can access it's contents in the same way you would a dictionary using d.get('top') or d['top']

How can I put all keys of file that contain key value pairs in quotes using python code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a file contain key value pairs below form:
[
{
af_NA: "Afrikaans (Namibia)",
af_ZA: "Afrikaans (South Africa)",
af: "Afrikaans",
ak_GH: "Akan (Ghana)",
ak: "Akan",
sq_AL: "Albanian (Albania)",
sq: "Albanian",
am_ET: "Amharic (Ethiopia)",
...
}
]
but keys are not in quotes. I want put all keys in quotes using python code.
Thanks
this looks like valid yaml, so you could use PyYaml to parse the file, then dump it to json
with open('your_file') as fl:
data = yaml.safe_load(fl)
print(json.dumps(data))

Is there a way to get all keys that match a string in a json file's values and output them to a text file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to get all keys' values that equal "url" ignoring nesting from a JSON file and then output them to a text file. How would I go about doing this?
I'm running Python 3.7 and cannot seem to find a solution.
r = requests.get('https://launchermeta.mojang.com/mc/game/version_manifest.json')
j = r.json()
The result expected from this would be a text file filled with links from this json file.
https://launchermeta.mojang.com/v1/packages/31fa028661857f2e3d3732d07a6d36ec21d6dbdc/a1.2.3_02.json
https://launchermeta.mojang.com/v1/packages/2dbccc4579a4481dc8d72a962d396de044648522/a1.2.3_01.json
https://launchermeta.mojang.com/v1/packages/48f077bf27e0a01a0bb2051e0ac17a96693cb730/a1.2.3.json
etc.
Using requests library
import requests
response = requests.get('https://launchermeta.mojang.com/mc/game/version_manifest.json').json()
url_list = []
for result in response['versions']:
url_list.append(result['url'])
print(url_list)

Data extract from text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Please help.
I have data in text file with 2 long columns tab spaced
I have to extract first and second columns separately
Can it be done with Python ?
If you need the columns as two separate lists:
first_column = []
second_column = []
data_file = open('your_file.txt')
for line in data_file.readlines():
first_column.append(line.split('\t')[0])
second_column.append(line.split('\t')[1])
data_file.close()

Python: getting info from file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Users are prompted for the location of a file that contains multiple sets of some information: the product number, the price of the product, the day, the month, the year.
eg.
12345678,200,1,1,2014
23456789,150,1,1,2014
12345678,180,1,2,2014
I need to get the total number of prices (ie 200,150,180). How do I do so?
Open your file, iterate over its lines with a for loop, split each line by commas and print second field.
This should work:
filepath = raw_input('Input file path:\n')
with open(filepath) as f:
for line in f:
print line.split(',')[1]

Categories

Resources