This question already has answers here:
Are multi-line strings allowed in JSON?
(15 answers)
Closed 3 years ago.
I'm trying to read a json file in python and I want to break a long value into multiples lines to make it easier to read it.
e.g JSON file:
mem's value is too long, so I'd like to make it multiple lines.
{
"resource":{
"cpu": ["host, usage_system, usage_user, usage_iowait", "1=1"],
"mem": ["host, active/(1024*1024) as active, available/(1024*1024) as available, available_percent, buffered/(1024*1024) as buffered, cached/(1024*1024) as cached, swap_cached/(1024*1024) swap_cached, swap_total/(1024*1024) swap_total, total/(1024*1024) mem_total, used/(1024*1024) mem_used", "1=1"],
"net": ["host, bytes_recv, bytes_sent, packets_recv, packets_sent", "1=1 and interface != 'all'"]
}
}
I want to make the value like below.
{ "resource":{
"cpu": ["host, usage_system, usage_user, usage_iowait", "1=1"],
"mem": ["host, active/(1024*1024) as active, available/(1024*1024) as available,
available_percent, buffered/(1024*1024) as buffered,
cached/(1024*1024) as cached, swap_cached/(1024*1024) swap_cached,
swap_total/(1024*1024) swap_total,
total/(1024*1024) mem_total, used/(1024*1024) mem_used", "1=1"],
"net": ["host, bytes_recv, bytes_sent, packets_recv, packets_sent", "1=1 and interface != 'all'"]
}
}
Thanks!
ok, so you are probably going to want to make a list of strings in json, then combine them with "".join(). For example,
json.json
{
"multiline": [
"this is a re",
"ally long st",
"ring, so I n",
"eed to make ",
"it one line"
]
}
multiline.py
import json
with open("json.json", "r") as jsonfile:
stringlist = json.loads(jsonfile.read())["multiline"]
result = "".join(stringlist)
print(result)
Related
This question already has answers here:
How do I get the last element of a list?
(25 answers)
Closed 1 year ago.
Here I'm trying to get the last m2id to print it into console.
[{
"m2id":"865199733949071370",
"author":{
"id":"862323352869797948"
}
},
{
"m2id":"865199658103078925",
"author":{
"id":"751742911682445312"
}
}]
And here's the code I wrote for it. (r.json being the json above):
data = r.json()
for id in data:
id3 = id["id"][-1]
print(id3)
Running the code gave me the last number of the first m2id, Which is not what I want.
Edit: data[-1]["id"] Worked. Thanks to everyone!
You can try this
x = [{
"m2id":"865199733949071370",
"author":{
"id":"862323352869797948"
}
},
{
"m2id":"865199658103078925",
"author":{
"id":"751742911682445312"
}
}]
print(x[len(x)-1]["m2id"])
Output
865199658103078925
This is a straight forward question, How to use python to process the log file (Consider it as a json string for now). Below is the json data:
{
"voltas": {
"ac": [
{
"timestamp":1590761564,
"is_connected":true,
"reconnection_status":"N/A"
},
{
"timestamp":1590761566,
"is_connected":true,
"reconnection_status":"N/A"
},
{
"timestamp":1590761568,
"is_connected":false,
"reconnection_status":"true"
},
{
"timestamp":1590761570,
"is_connected":true,
"reconnection_status":"N/A"
},
{
"timestamp":1590761572,
"is_connected":true,
"reconnection_status":"N/A"
},
{
"timestamp":1590761574,
"is_connected":false,
"reconnection_status":"false"
},
{
"timestamp":1590761576,
"is_connected":false,
"reconnection_status":"true"
}
]
}
}
Since the question is just regarding how to process the json data, I am skipping the discussion about the data in json. Now, what I need is the analysed data as below.
{
"voltas" : [
"ac": {
"number_of_actual_connection_drops": 3,
"time_interval_between_droppings": [4, 8],
"number_of_successful_reconnections": 2,
"number_of_failure_reconnections": 1
}
]
}
This is how the data is analysed:
"number_of_actual_connection_drops": Number of "is_connected" == false.
"time_interval_between_droppings": It is a list which will be populated from the end(append from beginning). We need to pick the time stamp of the item which will have "is_connected":false, and "reconnection_status":"true". In this case last(7th item) block with timestamp = 1590761576. Now we need fo find the timestamp of previous block with "is_connected":false, and "reconnection_status":"true". In this case it's 3rd item with timestamp 1590761568. Now the last item in the list is difference of this timestamps 8. Now the list is [8].
Now the timestamp is 1590761568 and we don't have any previous block with is_connected: false, and reconnection_status: true, so we will take the first items timestamp which is 1590761564 and now the difference is 4. So the list is [4, 8]
"number_of_successful_reconnections": Number of "reconnected_status" = true
"number_of_failure_connections": Number of "reconnected_status" = false
We can achieve this with for loops and some if conditions. I am interested in doing this using functional programming ways (reduce, map, filter) in python.
For simplification I have mentioned only "ac". There will be many items similar to this. Thanks.
I'm struggling with my json data that I get from an API. I've gone into several api urls to grab my data, and I've stored it in an empty list. I then want to take out all fields that say "reputation" and I'm only interested in that number. See my code here:
import json
import requests
f = requests.get('my_api_url')
if(f.ok):
data = json.loads(f.content)
url_list = [] #the list stores a number of urls that I want to request data from
for items in data:
url_list.append(items['details_url']) #grab the urls that I want to enter
total_url = [] #stores all data from all urls here
for index in range(len(url_list)):
url = requests.get(url_list[index])
if(url.ok):
url_data = json.loads(url.content)
total_url.append(url_data)
print(json.dumps(total_url, indent=2)) #only want to see if it's working
Thus far I'm happy and can enter all urls and get the data. It's in the next step I get trouble. The above code outputs the following json data for me:
[
[
{
"id": 316,
"name": "storabro",
"url": "https://storabro.net",
"customer": true,
"administrator": false,
"reputation": 568
}
],
[
{
"id": 541,
"name": "sega",
"url": "https://wedonthaveanyyet.com",
"customer": true,
"administrator": false,
"reputation": 45
},
{
"id": 90,
"name": "Villa",
"url": "https://brandvillas.co.uk",
"customer": true,
"administrator": false,
"reputation": 6
}
]
]
However, I only want to print out the reputation, and I cannot get it working. If I in my code instead use print(total_url['reputation']) it doesn't work and says "TypeError: list indices must be integers or slices, not str", and if I try:
for s in total_url:
print(s['reputation'])
I get the same TypeError.
Feels like I've tried everything but I can't find any answers on the web that can help me, but I understand I still have a lot to learn and that my error will be obvious to some people here. It seems very similar to other things I've done with Python, but this time I'm stuck. To clarify, I'm expecting an output similar to: [568, 45, 6]
Perhaps I used the wrong way to do this from the beginning and that's why it's not working all the way for me. Started to code with Python in October and it's still very new to me but I want to learn. Thank you all in advance!
It looks like your total_url is a list of lists, so you might write a function like:
def get_reputations(data):
for url in data:
for obj in url:
print(obj.get('reputation'))
get_reputations(total_url)
# output:
# 568
# 45
# 6
If you'd rather not work with a list of lists in the first place, you can extend the list with each result instead of append in the expression used to construct total_url
You can also use json.load and try to read the response
def get_rep():
response = urlopen(api_url)
r = response.read().decode('utf-8')
r_obj = json.loads(r)
for item in r_obj['response']:
print("Reputation: {}".format(item['reputation']))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
how parse multi-level json data, in Mongodb with Python 3.5?
PLAYER_DEFAULT = {
"_id": Inc(PlayersDB),
"mail": "test#gmail.com",
"password": "123456",
"token": uuid4().hex,
"username": "applet",
"user_info":
{
"show_name" : "Hero",
"rate_us": 20000,
"rating": 300,
"gcg": "",
"ration":0,
"items":
[
{"id":1, "item_type":"socket", "name":"aEveA", "data":{"level":1, "stat":"AVA"}},
{"id":2, "item_type":"socket", "name":"aEveA", "data":{"level":4, "stat":"AVA"}},
{"id":3, "item_type":"socket", "name":"Hestos", "data":{"level":9, "stat":"Hest"}},
{"id":4, "item_type":"user", "name":"AAACZX", "data":{"expr":1000}},
{"id":5, "item_type":"user", "name":"AAAAZZZCX", "data": {"expr":1000}}
]
}
}
how get data level and stat in items?
["_id"]->["show_name"]
["_id"]->["user_info"]->["items"]
["_id"]->["user_info"]->["items"] -> get value by "stat" in items[0]
me need get items id, by items "stat" value
i use loop
how get x count?
for x in PlayersDB.find({"_id":1}):
print(x["user_info"]["items"][x.count()])
And... how to update "item_type", by "id", in "items"?
And how to delete 1 docuement by "id":1 in "items"?
For that document, try:
doc['user_info']['items'][0]['data']['stat']
If you have a doc for a player, and want to count how many items that player has, you will use some built-in Python function, which I will call XXX, and which you should research to see what that function is, and use it on the items attribute of the doc:
number_of_items = XXX(doc['user_info']['items'])
And in future, please use the same names and structure in your question as in your posted examples.
I am using the yelp dataset and I want to parse the review json file to a dictionary. I tried loading it on a pandas DataFrame and then creating the dictionary, but because the file is too big it is time consuming. I want to keep only the user_id and stars values. A line of the json file looks like this:
{
"votes": {
"funny": 0, "useful": 2, "cool": 1},
"user_id": "Xqd0DzHaiyRqVH3WRG7hzg",
"review_id": "15SdjuK7DmYqUAj6rjGowg", "stars": 5, "date": "2007-05-17",
"text": (
"dr. goldberg offers everything i look for in a general practitioner. "
"he's nice and easy to talk to without being patronizing; he's always on "
"time in seeing his patients; he's affiliated with a top-notch hospital (nyu) "
"which my parents have explained to me is very important in case something "
"happens and you need surgery; and you can get referrals to see specialists "
"without having to see him first. really, what more do you need? i'm "
"sitting here trying to think of any complaints i have about him, but i'm "
"really drawing a blank."
),
"type": "review", "business_id": "vcNAWiLM4dR7D2nwwJ7nCA"
}
How can i iterate over every 'field' (for the lack o a better word)? So far i can only iterate over each line.
EDIT
As requested pandas code :
reading the json
with open('yelp_academic_dataset_review.json') as f:
df = pd.DataFrame(json.loads(line) for line in f)
Creating the dictionary
dict = {}
for i, row in df.iterrows():
business_id = row['business_id']
user_id = row['user_id']
rating = row['stars']
key = (business_id, user_id)
dict[key] = rating
You don't need to read this into a DataFrame. json.load() returns a dictionary. For example:
sample.json
{
"votes": {
"funny": 0,
"useful": 2,
"cool": 1
},
"user_id": "Xqd0DzHaiyRqVH3WRG7hzg",
"review_id": "15SdjuK7DmYqUAj6rjGowg",
"stars": 5,
"date": "2007-05-17",
"text": "dr. goldberg offers everything i look for in a general practitioner. he's nice and easy to talk to without being patronizing; he's always on time in seeing his patients; he's affiliated with a top-notch hospital (nyu) which my parents have explained to me is very important in case something happens and you need surgery; and you can get referrals to see specialists without having to see him first. really, what more do you need? i'm sitting here trying to think of any complaints i have about him, but i'm really drawing a blank.",
"type": "review",
"business_id": "vcNAWiLM4dR7D2nwwJ7nCA"
}
read_json.py
import json
with open('sample.json', 'r') as fh:
result_dict = json.load(fh)
print(result_dict['user_id'])
print(result_dict['stars'])
output
Xqd0DzHaiyRqVH3WRG7hzg
5
With that output you can easily create a DataFrame.
There are several good discussions about parsing json as a stream on SO, but the gist is it's not possible natively, although some tools seem to attempt it.
In the interest of keeping your code simple and with minimal dependencies, you might see if reading the json directory into a dictionary is a sufficient improvement.