How to get value from particular json format in python - python-2.7

I have a json in this format and I want the value of id and path . I
have written the following script given below after json format
.Please help me with a solution.
{
"metaData": {
"root": "queue",
"queueStatus": {
"agentName": "publish-u11cmspu1",
"agentId": "publish-u11cmspu1",
"isBlocked": false,
"isPaused": true,
"time": 1524644055974,
"processingSince": 0,
"lastProcessTime": 1524644046549,
"nextRetryPeriod": -1524644055975
},
"fields": [
"id",
"path",
"time",
"userid",
"type",
"size",
"lastProcessed",
"numProcessed"
]
},
"queue": [{
"id": "anon/7fe264f5-24c6-42df-8409-866783641ca3/2018/4/25/8/14/com.day-u11cmspu1_785",
"path": "/content/lh/am/en/Homepage2014",
"time": 1524644051651,
"userid": "admin",
"type": "Activate",
"size": 8781,
"lastProcessed": 0,
"numProcessed": 0
}
]
Code Below :
url ='http://localhost:41102/etc/replication/agents.publish/mobile_notification_agent/jcr:content.queue.json?_dc=1524389867464&agent=mobile_notification_agent'
username='admin'
password='admin'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(url)
page = pagehandle.read()
json_response=json.loads(page)
queueid_list = []
for queueid in (json_response['queue']):
if queueid['type']=='delete':
queueid_list.append(queueid['id'])
print queueid_list

Related

Get Absolute URL of image using django-treebeard tree responce

Hello currently I implemented django-treebeard package for archive tree structure for categories.
Here I faced problem in response with URL of image.
I can't see Absolute URL of image categories
NOTE - serializer is not working with this recursive tree structure. I already know that you can pass
context = {'request':request} in serializer but in my case, it's not working because it is recursive tree structure.
views.py
class CategoryAPI(APIView):
def get(self, request):
try:
if CategoryModel.objects.count() > 0:
categories = CategoryModel.dump_bulk()
# serializer = CategorySerializer(instance=categories, many=True)
# print(serializer)
return Response({"status":True,"categories":categories}, status=status.HTTP_200_OK)
else:
return Response({'message': 'Categories not found.'}, status=status.HTTP_200_OK)
except Exception as e:
return Response({'error':str(e)})
API GET Response is like this
{
"status": true,
"categories": [
{
"data": {
"name": "Fruits",
"image": "Categories/download_3_Vg6Rv37.jfif"
},
"id": 1,
"children": [
{
"data": {
"name": "Apple",
"image": "Categories/download_6zbqDvn.jfif"
},
"id": 2,
"children": [
{
"data": {
"name": "Green Kasmiri Apple",
"image": "Categories/download_2_HaMCXyc.jfif"
},
"id": 10
}
]
},
{
"data": {
"name": "Banana",
"image": "Categories/download_4_Mn5h9nL.jfif"
},
"id": 3
},
{
"data": {
"name": "Mango",
"image": "Categories/download_5_4RkBFcS.jfif"
},
"id": 5
},
{
"data": {
"name": "Orange",
"image": "Categories/download_6_vWVEQgm.jfif"
},
"id": 4
}
]
},
{
"data": {
"name": "Health",
"image": "Categories/images_fwp1mBB.jfif"
},
"id": 6
}
]
}
I want to Responce is like this
{
"status": true,
"categories": [
{
"data": {
"name": "Fruits",
"image": "http://127.0.0.1:8000/media/Categories/download_3_Vg6Rv37.jfif"
},
"id": 1,
"children": [
{
"data": {
"name": "Apple",
"image": "http://127.0.0.1:8000/media/Categories/download_6zbqDvn.jfif"
},
"id": 2,
"children": [
{
"data": {
"name": "Green Kasmiri Apple",
"image": "http://127.0.0.1:8000/media/Categories/download_2_HaMCXyc.jfif"
},
"id": 10
}
]
},
{
"data": {
"name": "Banana",
"image": "http://127.0.0.1:8000/media/Categories/download_4_Mn5h9nL.jfif"
},
"id": 3
},
{
"data": {
"name": "Mango",
"image": "http://127.0.0.1:8000/media/Categories/download_5_4RkBFcS.jfif"
},
"id": 5
},
{
"data": {
"name": "Orange",
"image": "http://127.0.0.1:8000/media/Categories/download_6_vWVEQgm.jfif"
},
"id": 4
}
]
},
{
"data": {
"name": "Health",
"image": "http://127.0.0.1:8000/media/Categories/images_fwp1mBB.jfif"
},
"id": 6
}
]
}

Power Query M. Json array of key-pair transform in Column

Thanks for your help,
I'm trying to transform the Array called 'Tags' hosting a list of pair of key-values into a list of columns: CustomerId, CustomerDisplayName, CustomerPath where each list contains a respective value being a store for each charge.
{
"periodFrom": "2020-11-09T00:00:00",
"periodTo": "2020-12-08T00:00:00",
"charges": [
{
"listPrice": 5.05,
"netPrice": 5.05,
"netPriceProrated": 5.05,
"subTotal": 5.05,
"currency": "CAD",
"isBilled": true,
"isProratable": true,
"deductions": [],
"fees": [],
"invoice": {
"number": "2822835",
"date": "2020-11-16T00:00:00",
"periodFrom": "2020-10-09T00:00:00",
"periodTo": "2020-11-08T00:00:00"
},
"taxes": [
{
"name": "GST",
"appliedRate": 5.0
},
{
"name": "QST",
"appliedRate": 9.975
}
],
"tags": [
{
"name": "CustomerId",
"value": "42c8edf4-365a-4068-bde6-33675832afbb"
},
{
"name": "CustomerDisplayName",
"value": "Blue Sky Group"
},
{
"name": "CustomerPath",
"value": "Devmesh Co-Branded/Blue Sky Group"
}
]
}
]
}
Here the actual snippet of code I'm actually
let
...
Response2 = Table.FromRecords( { Response } ),
#"Expand1" = Table.ExpandListColumn(Response2, "charges"),
#"Expand2" = Table.ExpandRecordColumn(#"Expand1", "charges", {"productId", "productName", "sku", "chargeId", "chargeName", "chargeType", "periodFrom", "periodTo", "quantity", "listPrice", "netPrice", "netPriceProrated", "subTotal", "currency", "isBilled", "isProratable", "deductions", "fees", "invoice", "taxes", "tags"}, {"charges.productId", "charges.productName", "charges.sku", "charges.chargeId", "charges.chargeName", "charges.chargeType", "charges.periodFrom", "charges.periodTo", "charges.quantity", "charges.listPrice", "charges.netPrice", "charges.netPriceProrated", "charges.subTotal", "charges.currency", "charges.isBilled", "charges.isProratable", "charges.deductions", "charges.fees", "charges.invoice", "charges.taxes", "charges.tags"})
in
#"Expand2"

How to check Key/Value exists in List of Dictionary in Python?

For given a dictionary of list of dictionary below, I would like to check if key value "url": "/Province/?ID=83" exist in a list of dictionary or not.
data = {
"current_page": 1,
"data": [
{
"columns": [
{
"key": "ID",
"value": "83"
},
{
"key": "Description",
"value": "Test Curring"
},
{
"key": "LocalDescription",
"value": "tests curring"
}
],
"important": [
{
"key": "ID",
"value": "83"
},
{
"key": "Description",
"value": "Test Curring"
},
{
"key": "LocalDescription",
"value": "tests curring"
}
],
"url": "/Province/?ID=83"
},
{
"columns": [
{
"key": "ID",
"value": "82"
},
{
"key": "Description",
"value": "Test 81 Description"
},
{
"key": "LocalDescription",
"value": "Local Test Description"
}
],
"important": [
{
"key": "ID",
"value": "82"
},
{
"key": "Description",
"value": "Test 81 Description"
},
{
"key": "LocalDescription",
"value": "Local Test Description"
}
],
"url": "/Province/?ID=82"
},
]
}
I know it exist, how can I check to return True or False for its existence?
I tried:
list_data = data.get('data')
index = 0
for item in list_data:
url = item[0].get('url')
index += 1
print 'url: ',url
However, I got error:
url = item[0].get('url')
KeyError: 0
This works by gathering url of each item in data['data'] and seeing if the desired value is in it; it assumes every item in data['data'] has a url element.
"/Province/?ID=83" in [x['url'] for x in data['data']]
Assuming that the schema of data is consistent, you can try this:
It will return True if key:value exist in data, else it will return False.
def check(data, key, value):
for i in data['data']:
try:
if(i[key]==value): return True
except:
pass
return False

How do I make an User required JSON

I have a JSON file, in that three objects are available, In that 2nd and 3rd objects does not have some fields which I actually needed. In missing fields, I need to add my own values. I will provide my code below
I tried this So far:
with open("final.json") as data1:
a = json.load(data1)
final = []
for item in a:
d = {}
d["AppName"]= item["name"]
d["AppId"] = item["id"]
d["Health"] = item["health"]
d["place1"] = item["cities"][0]["place1"]
d["place2"] = item["cities"][0]["place2"]
print(final)
Error: I am getting Key Error
My Input JSON file has similar data:
[{
"name": "python",
"id": 1234,
"health": "Active",
"cities": {
"place1": "us",
"place2": "newyork"
}
},
{
"name": "java",
"id": 2345,
"health": "Active"
}, {
"name": "python",
"id": 1234
}
]
I am expecting output:
[{
"name": "python",
"id": 1234,
"health": "Active",
"cities": {
"place1": "us",
"place2": "newyork"
}
},
{
"name": "java",
"id": 2345,
"health": "Null",
"cities": {
"place1": "0",
"place2": "0"
}
}, {
"name": "python",
"id": 1234,
"health": "Null",
"cities": {
"place1": "0",
"place2": "0"
}
}
]
I see two issues with the code that you have posted.
First, you are referring to the 'cities' field in you input JSON as if it is a list when it is, in fact, an object.
Second, to handle JSON containing objects which may be missing certain fields, you should use the Python dictionary get method. This method takes a key and an optional value to return if the key is not found (default is None).
for item in a:
d = {}
d["AppName"]= item["name"]
d["AppId"] = item["id"]
d["Health"] = item.get("health", "Null")
d["place1"] = item.get("cities", {}).get("place1", "0")
d["place2"] = item.get("cities", {}).get("place2", "0")

Schema validation error

I have used a field 'timestamp' is of type 'datetime.datetime' used in models.py in one of the django project,
Here is data :
{"bedId": "5807a14", "description": "",
"timestamp": "2013-09-16T15:40:16.383133", "encounterId": null, "patientId": null, "type":
"end_of_shift_note", "triggeredBy": "abc"}
Here is schema :
schema =
{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required":true,
"properties":{
"type": {
"type":"string",
"id": "http://jsonschema.net/bedNumber",
"required":true
},
"encounterId": {
"type":"string",
"id": "http://jsonschema.net/encounterId",
"required":true
},
"timestamp": {
"type" :["null","string","date-time"],
'required' :false
},
"bedId": {
"type":"string",
"id": "http://jsonschema.net/cleaningStatus",
"required":true
},
"patientId": {
"type":["string","null"],
"id": "http://jsonschema.net/facilityId",
"required":true
} ,
"id": {
"type":["string","null"],
"id": "http://jsonschema.net/id",
"required":false
},
"triggeredBy": {
"type":["string","null"],
"id": "http://jsonschema.net/lastCleanedTime",
"required":false
}
}
}
On doing schema validation : https://pypi.python.org/pypi/json-schema-validator
def assertDataMatchesSchema(self, data, schema_file_name):
with open(os.path.join("hma/resource_jsonschema", schema_file_name)) as schema_file:
try:
schema = json.load(schema_file)
validate(data, schema)
except Exception as e:
print "Schema validation Error Message :",e.message
It is giving error on terminal:
Schema validation Error Message : Expecting property name: line 19 column 5 (char 401)
Problem is simple: What would be the type format for timestamp used in above jsonschema?
Please help
Got your problem:-
You are giving type instead of format.
Following example is working for me:-
data = {"timestamp": "2013-09-16T15:40:16.21211"}
schema ="""{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required":true,
"properties":{
"timestamp": {
"format" :"date-time",
"type":"string",
"required" :false
}
}}"""
jsonschema.validate(data,json.loads(schema))