Learning Days
Code to the get the data in JSON Format
#...
cursor.execute("SELECT * FROM user")
response = {
"version": "5.2",
"user_type": "online",
"user": list(cursor),
}
response = json.dumps(response, sort_keys=False, indent=4, separators=(',', ': '))
print(response)
# ...
This produces output as
{
"version": "5.2",
"user_type": "online",
"user":
[
{
"name": "John",
"id": 50
},
{
"name": "Mark",
"id": 57
}
]
}
print(response["user"]) - TypeError: string indices must be integers
How do i access the values in JSON
json.dumps return a string, need a small conversion something like this, not sure is this the exact method to do
Solution:
response = JSONEncoder().encode(response )
response = JSONDecoder().decode(response )
response = json.loads(response )
print(response['user'[0]['id'])
Related
I have about 100 JSON files, all titled with different dates and I need to merge them into one CSV file that has headers "date", "real_name", "text".
There are no dates listed in the JSON itself, and the real_name is nested. I haven't worked with JSON in a while and am a little lost.
The basic structure of the JSON looks more or less like this:
Filename: 2021-01-18.json
[
{
"client_msg_id": "xxxx",
"type": "message",
"text": "THIS IS THE TEXT I WANT TO PULL",
"user": "XXX",
"user_profile": {
"first_name": "XXX",
"real_name": "THIS IS THE NAME I WANT TO PULL",
"display_name": "XXX",
"is_restricted": false,
"is_ultra_restricted": false
},
"blocks": [
{
"type": "rich_text",
"block_id": "yf=A9",
}
]
}
]
So far I have
import glob
read_files = glob.glob("*.json")
output_list = []
all_items = []
for f in read_files:
with open(f, "rb") as infile:
output_list.append(json.load(infile))
data = {}
for obj in output_list[]
data['date'] = f
data['text'] = 'text'
data['real_name'] = 'real_name'
all_items.append(data)
Once you've read the JSON object, just index into the dictionaries for the data. You might need obj[0]['text'], etc., if your JSON data is really in a list in each file, but that seems odd and I'm assuming your data was pasted from output_list after you'd collected the data. So assuming your file content is exactly like below:
{
"client_msg_id": "xxxx",
"type": "message",
"text": "THIS IS THE TEXT I WANT TO PULL",
"user": "XXX",
"user_profile": {
"first_name": "XXX",
"real_name": "THIS IS THE NAME I WANT TO PULL",
"display_name": "XXX",
"is_restricted": false,
"is_ultra_restricted": false
},
"blocks": [
{
"type": "rich_text",
"block_id": "yf=A9",
}
]
}
test.py:
import json
import glob
from pathlib import Path
read_files = glob.glob("*.json")
output_list = []
all_items = []
for f in read_files:
with open(f, "rb") as infile:
output_list.append(json.load(infile))
data = {}
for obj in output_list:
data['date'] = Path(f).stem
data['text'] = obj['text']
data['real_name'] = obj['user_profile']['real_name']
all_items.append(data)
print(all_items)
Output:
[{'date': '2021-01-18', 'text': 'THIS IS THE TEXT I WANT TO PULL', 'real_name': 'THIS IS THE NAME I WANT TO PULL'}]
I have a socket response that is this:
{"op":0,"d":{"author":{"id":"6699457769390473216","name":"Test","verified":false},"unixTime":1597277057132,"id":"6699465549836976128","group":"64632423765273287342","content":"Yo","_id":"5f34838198980c0023fa49e3"},"t":"MESSAGE"}
and I need to access the "d" object, I've tried doing
print(JSON(data)["d"])
and it just Returns null every time.
If data is of type String, you are probably using the wrong init method to initialize the JSON object. Try using init(parseJSON:) like this:
let jsonString = """
{
"op": 0,
"d": {
"author": {
"id": "6699457769390473216",
"name": "Test",
"verified": false
},
"unixTime": 1597277057132,
"id": "6699465549836976128",
"group": "64632423765273287342",
"content": "Yo",
"_id": "5f34838198980c0023fa49e3"
},
"t": "MESSAGE"
}
"""
let json = JSON(parseJSON: jsonString)
print(json["d"])
JSON Response:
[
{
"actNum": "12345678",
"prodType": "Test",
"period": {
"January": [
{
"name": "Jake",
"rRar": 12.34,
"lRar": 340.45,
"address": "New York"
},
{
"name": "Jorge",
"rRar": 28.78,
"lRar": 250.49,
"address": "Chicago"
}
]
}
}
]
I have to verify that numeric fields in the above response rRar and lRar should have value till two decimal points like 12.78,32.56.
Could anyone please help me if this validation can be done using Karate API?
Here you go:
* def nums = $response..rRar
* def temp = $response..lRar
* karate.appendTo(nums, temp)
* def strs = karate.map(nums, function(x){ return x + '' })
* match each strs == '#regex [0-9].+\\.[0-9]{2}'
This is the code used to fetch data from DB
import pymysql
import json
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
cursor.execute("SELECT * FROM user")
rows = []
for row in cursor:
rows += [row]
print(json.dumps(rows, sort_keys=False, indent=4, separators=(',', ': ')))
cursor.close()
conn.close()
Output in json is -
[
{
"name": "John",
"id": 50
},
{
"name": "Mark",
"id": 57
}
]
But I want the output in this format -
{
"version": "5.2",
"user_type": "online",
"user":
[
{
"name": "John",
"id": 50
},
{
"name": "Mark",
"id": 57
}
]
}
where the version and user_type can be manually entered or appended to the result.
Simply wrap the result set in a dict of your liking then.
# ...
cursor.execute("SELECT * FROM user")
response = {
"version": "5.2",
"user_type": "online",
"user": list(cursor), # This is equivalent to iterating over the cursor yourself.
}
print(json.dumps(response, sort_keys=False, indent=4, separators=(',', ': ')))
# ...
You can create a dict with the version, the user type, and the user (where for the key 'user' you enter rows as the value). Then convert that to json using json.dump or json.dumps:
data = { "version": "5.2", "user_type": "online", "user":rows }
print(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': ')))
I am getting the results from my mongoDB database and trying to send them as a JSON object through curl:
custos = customerDB.CustomerModel.find()
jsonCusto = []
for doc in custos:
temp = json.dumps(doc, default=json_util.default)
jsonCusto.append(temp)
I print this before send and get:
'{"firstName": "Joshu", "lastName": "Wak", "creation": {"$date": 1414531609314}, "Cust_ID": 101, "streetNo": "3231", "_id": {"$oid": "54500a19d0f6120a0021c879"}, "email": "lolazo#cvn.com", "streetName": "washingoton"}'
but on the curl screen I get:
"{\"firstName\": \"Joshu\", \"lastName\": \"Wak\", \"creation\": {\"$date\": 1414531609314}, \"Cust_ID\": 101, \"streetNo\": \"3231\", \"_id\": {\"$oid\": \"54500a19d0f6120a0021c879\"}, \"email\": \"lolazo#cvn.com\", \"streetName\": \"washingoton\"}
I tried about 10 combinations and cannot get it right. I would appreciate any help.
Thanks!
You almost certainly are double-encoding your JSON. Simply remove the json.dumps in your loop and only use it at the end:
custos = customerDB.CustomerModel.find()
jsonCusto = list(custos)
jsonText = json.dumps(jsonCusto, default=json_util.default)
I had same challenge. In my case I wrote some_schema.dumps() instead of some_schema.dump()
Below is the output of author_schema.dumps():
#app.route('/authors', methods = ['GET'])
def index():
get_authors = Authors.query.all()
author_schema = AuthorsSchema(many=True)
authors= author_schema.dump(get_authors)
return make_response(jsonify({"authors": authors}))
Output:
{
"authors": "[{\"specialisation\": \"Joke Developer\", \"name\": \"Kaunda Ibn Ahmed\", \"id\": 1.0}, {\"specialisation\": \"History\", \"name\": \"Akademic Vuvuzela\", \"id\": 2.0}]"
}
Below is the output of author_schema.dump():
#app.route('/authors', methods = ['GET'])
def index():
.....
.....
authors= author_schema.dump(get_authors)
return make_response(jsonify({"authors": authors}))
Output:
{
"authors": [
{
"id": 1.0,
"name": "Kaunda Ibn Ahmed",
"specialisation": "Joke Developer"
},
{
"id": 2.0,
"name": "Akademic Vuvuzela",
"specialisation": "History"
}
]
}