I am making JSON structure.
I need access from id3 to id4 (one step forward)
I need access from id4 to id3 (one step back)
I have to be able to search into children elements
Not using format json_result[1]["next"]["next"][1]["next"][0]["Name"]
Link to a source with exact lesson will be fine - I couldn't find one :(
Thank you.
json_result = json
json_last = json_result # Using json_last as pointer to a record into json_result
. . .
# part of a cycle reading doc
json_result_temp = {
"Name":item_name_clean,
"id":item_autoname,
"lvl":json_structure_lvl,
#"prev": {},
"prev": int,
"next": {}
}
if json_structure_lvl == 0:
json_result = json_result_temp # writing first element
json_last = json_result # first element don't have previuos elements
else:
#json_result_temp["prev"] = json_last # get a error when trying to convert into JSON
json_result_temp["prev"] = int(id(json_last)) # trying to store adress of previous element
json_last["next"] = json_result_temp # adding current record "json_result_temp" into "next" field of previus record
json_last = json_last["next"] # changing point to the child element
[{
"Name": "Name 1",
"id": "id0",
"lvl": 0,
"prev": {},
"next": {
"Name": "Name 1.1",
"id": "id1",
"lvl": 1,
"prev": 2536898825856,
"next": [
{"Name": "Name 1.1.1",
"id": "id2",
"lvl": 2,
"prev": 2536898748864,
"next": {}},
{"Name": "Name 1.1.2",
"id": "id3",
"lvl": 2,
"prev": 2536898823424,
"next": [
{"Name": "Name 1.1.2.1",
"id": "id4",
"lvl": 3,
"prev": 2536898827072,
"next": {}},
{"Name": "Name 1.1.2.2",
"id": "id5",
"lvl": 3,
"prev": 2536898828782,
"next": {}}
]}
]
},
{ "Name": "Name 2",
"id": "id6",
"lvl": 0,
"prev": {},
"next": {
"Name": "Name 2.1",
"id": "id7",
"lvl": 1,
"prev": 2536898825856,
"next": [
{"Name": "Name 2.1.1",
"id": "id8",
"lvl": 2,
"prev": 2536898748864,
"next": {}},
{"Name": "Name 2.1.2",
"id": "id9",
"lvl": 2,
"prev": 2536898823424,
"next": [
{"Name": "Name 2.1.2.1",
"id": "id10",
"lvl": 3,
"prev": 2536898827072,
"next": {}},
{"Name": "Name 2.1.2.2",
"id": "id11",
"lvl": 3,
"prev": 2536898827888,
"next": {}}
]}
]
}
}]
I made "prev" and try to store there addresses location for the previuos element, but when I try to convert the results into the JSON format I get an error. How can I get access to the previous elements not using format json_result[1]["next"]["next"][1]["next"][0]["Name"], but using only pointer or something else. Because depth of children elements can be changed, and I don't know the exact path, I have to search one by one.
Related
I am trying to update the json payload with a dict type info and keeping the key position the same as before as it is required by the task I am working on.
Note I understand that the implementation of type dict not allow duplicate keys, but I do need this done, so any work-around or hacky approach would helps.
I have a payload which I loaded from a json file
payload.json
{
"name": "",
"address": "",
"age": " ",
"ethnicities": "",
"select": "",
"sub-ethnicities": "",
"select": "",
"option1": "",
"option2": ""
}
loading it
payload = json.load(open("payload.json"))
The I have the info:
info = {
"name": "Spock",
"ethnicities": "Vulcan",
"select": "paternal",
"sub-ethnicities": "human",
"select": "maternal",
}
I am trying to insert above info into the payload and keeping the key indexes the way they were.
Expected result would be
{
"name": "Spock",
"address": "",
"age": "",
"ethnicities": "Vulcan",
"select": "paternal",
"sub-ethnicities": "human",
"select": "maternal",
"option1": "",
"option2": ""
}
Thank you in advantage.
Using data | info or data.update(info) will removes the duplicate keys, which defeating the goal I am trying to archieve.
For example expected result is:
{
"name": "Spock",
"address": "",
"age": "",
"ethnicities": "Vulcan",
"select": "paternal",
"sub-ethnicities": "human",
"select": "maternal",
"Extra-ethnicities": "Asian",
"select": "Asian"
}
but I got was the duplicate keys got removed.
"name": "Spock",
"address": "",
"age": "",
"ethnicities": "Vulcan",
"select": "paternal",
"sub-ethnicities": "human",
"Extra-ethnicities": "Asian",
}
An operator for this was added in Python 3.9 as the Union operator:
payload_with_info = payload | info
print(payload_with_info)
>>>
{
'address': '',
'age': ' ',
'ethnicities': 'Vulcan',
'name': 'Spock',
'option1': '',
'option2': '',
'select': 'maternal',
'sub-ethnicities': 'human'
}
Can anyone help me solve this,
**I am kinda trying for some days.
I have this kind of JSON File**
[
{
"city": "Bucharest",
"lat": "44.4000",
"lng": "26.0833",
"country": "Romania",
"iso2": "RO",
"admin_name": "Bucureşti",
"capital": "primary",
"population": "1883425",
"population_proper": "1883425"
},
{
"city": "Timişoara",
"lat": "45.7597",
"lng": "21.2300",
"country": "Romania",
"iso2": "RO",
"admin_name": "Timiş",
"capital": "admin",
"population": "319279",
"population_proper": "319279"
},
I want to change or add the values on the other JSON file below.
For example if the admin_name from the upper JSON file does not exist in "locations" create it with all the options below "name": Name of the city , else if it matches with the "name" of the json file below he should Create a new "child" with {"name" : Name of the CITY, "order": 0},
{
"locations": [
{
"name": "Bucureşti",
"slug": "",
"description": "Judet",
"meta": [
{"image_id" : 45},
{"_icon" : ""}
],
"order": 0,
"child": [
{"name": "Bucureşti", "order": 1 },
{"name" : "Voluntari", "order": 2},
{"name" : "Fundeni", "order": 3},
{"name" : "Rosu", "order": 4}
]
}
]
}
I am try to use python to extract value, but I found a weird result.
Following is part of my json variable
"weatherElement": [
{
"elementName": "ELEV",
"elementValue": {
"value": "20.0"
}
},
{
"elementName": "TEMP",
"elementValue": {
"value": "25.0"
}
},
{
"elementName": "D_TNT",
"elementValue": {
"value": "2019-11-22T02:10:00+08:00"
}
}
],
and following code is correct for getting value 25.0 which is temperature
for unit in data['records']['location']:
# print(type(unit)) <---- output <dict>
if unit['stationId'] == 'C0V490':
for wea_unit in unit['weatherElement']: # unit['weatherElement'] is list
if wea_unit['elementName'] == 'TEMP':
print(type(wea_unit['elementValue'])) # is str
return wea_unit['elementValue']
My question is why type(wea_unit['elementValue']) is str?
I think it should be dict and I should use wea_unit['elementValue']['value'] to get '25.0', but it is wrong. Are there anyone know what mistake I made? Thanks!
edit:
following is example code which can run directly
import json
def parse_json_to_dataframe(data):
for unit in data['records']['location']:
# print(type(unit))
if unit['stationId'] == 'C0A560':
for wea_unit in unit['weatherElement']: # unit['weatherElement'] is list
if wea_unit['elementName'] == 'TEMP':
print(type(wea_unit['elementValue']))
return wea_unit['elementValue']
v = {"success":"true",
"result": {"resource_id": "O-A0001-001",
"fields": [{"id": "lat", "type": "Double"},
{"id": "lon", "type": "Double"},
{"id": "locationName", "type": "String"},
{"id": "stationId", "type": "String"},
{"id": "description", "type": "String"},
{"id": "elementName", "type": "String"},
{"id": "elementValue", "type": "Double"},
{"id": "parameterName", "type": "String"},
{"id": "parameterValue", "type": "String"}]}, # result end
"records": {"location": [{"lat": "24.778333",
"lon": "121.494583",
"locationName": "福山",
"stationId": "C0A560",
"time": {"obsTime": "2019-11-22 22:00:00"},
"weatherElement": [
{"elementName": "ELEV", "elementValue": "405.0"},
{"elementName": "WDIR", "elementValue": "0"},
{"elementName": "WDSD", "elementValue": "0.0"},
{"elementName": "TEMP", "elementValue": "19.6"}],
"parameter": [
{"parameterName": "CITY_SN", "parameterValue": "06"},
{"parameterName": "TOWN_SN", "parameterValue": "061"}]}]}}
temp = parse_json_to_dataframe(v)
print(temp)
I have a json and I want to get a element of it by python. the json look like this:
{"$id":"1","Data":
[{"$id":"2","ArticleId":39511,"Title":"a","Order":39,"TotalCount":0},
{"$id":"3","ArticleId":345921,"Title":"a","Order":21,"TotalCount":0},
{"$id":"4","ArticleId":43289,"Title":"a","Order":18,"TotalCount":0},
{"$id":"5","ArticleId":39540,"Title":"a","Order":16,"TotalCount":0},
{"$id":"6","ArticleId":963167,"Title":"a","Order":15,"TotalCount":0},
{"$id":"7","ArticleId":519284,"Title":"a","Order":14,"TotalCount":0},
{"$id":"8","ArticleId":109699,"Title":"a","Order":13,"TotalCount":0},
{"$id":"9","ArticleId":43466,"Title":"a","Order":12,"TotalCount":0},
{"$id":"10","ArticleId":39510,"Title":"a","Order":10,"TotalCount":0},
{"$id":"11","ArticleId":43307,"Title":"a","Order":9,"TotalCount":0},
{"$id":"12","ArticleId":715048,"Title":"a","Order":9,"TotalCount":0},
{"$id":"13","ArticleId":133952,"Title":"a","Order":8,"TotalCount":0},
{"$id":"14","ArticleId":459712,"Title":"a","Order":8,"TotalCount":0},
{"$id":"15","ArticleId":11503,"Title":"a","Order":7,"TotalCount":0}],"TotalCount":
14,"Id":"0","IsSuccess":true,"Message":"OK","StatusCode":200,"ValidationMessages":
null,"Exception":null}
In python I use:
mydata = json.loads(response.text)
now I want to know how to use mydata to get the strings. I mean "a"
You can do it with:
mydata = json.loads(response_text)
titles = [dct['Title'] for dct in mydata['Data']]
A good way to help figure things like this out is to first do something like this:
print(json.dumps(mydata, indent=4))
Which, in this case, results in something like this being displayed:
{
"Exception": null,
"$id": "1",
"ValidationMessages": null,
"IsSuccess": true,
"TotalCount": 14,
"Message": "OK",
"Data": [
{
"$id": "2",
"TotalCount": 0,
"ArticleId": 39511,
"Order": 39,
"Title": "a"
},
{
"$id": "3",
"TotalCount": 0,
"ArticleId": 345921,
"Order": 21,
"Title": "a"
},
..., etc,
{
"$id": "15",
"TotalCount": 0,
"ArticleId": 11503,
"Order": 7,
"Title": "a"
}
],
"Id": "0",
"StatusCode": 200
}
From that, it usually becomes relatively easy to determine what you need to do to access any part or parts of the object.
I'm working in an application in Python. I have to do a GET request to get specific information. My code is somethings like that:
...
conn = httplib.HTTPConnection(self.url)
header = {"Authorization":"Bearer "+self.token}
conn.request("GET","/data",headers=header)
...
and the JSON that I obtain is similar to this (you can observe that there are 2 big different part... this is just an example, in my application the parts are a lots).
[
{
"createdAt": "2015-11-26T10:06:05.756Z",
"date": "2015-10-31T23:00:00.000Z",
"files": [],
"id": 1,
"metadata": {},
"notes": "note impianto 1",
"parentSubject": {
"code": "soggetto1",
"createdAt": "2015-11-26T10:05:38.765Z",
"id": 1,
"metadata": {},
"notes": "note soggetto 1",
"personalInfo": 1,
"sex": "M",
"tags": null,
"type": 1,
"updatedAt": "2015-11-26T10:05:38.765Z"
}
},
{
"createdAt": "2015-11-26T10:06:36.684Z",
"date": "2015-11-01T23:00:00.000Z",
"files": [],
"id": 2,
"metadata": {},
"notes": "note impianto 2",
"parentSubject": {
"code": "soggetto1",
"createdAt": "2015-11-26T10:05:38.765Z",
"id": 1,
"metadata": {},
"notes": "note soggetto 1",
"personalInfo": 1,
"sex": "M",
"tags": null,
"type": 1,
"updatedAt": "2015-11-26T10:05:38.765Z"
}
}
]
If for example I make this request:
...
conn.request("GET","/data?id=1",headers=header)
...
I obviously get only the first part. The problem is that I don't want to get all data that have id=1 but all data that have code=soggetto1. How can I do?
If API can't do this then you have to get all data and find on your own.
In example I changed one code to "soggetto2"
data = '''[
{
"createdAt": "2015-11-26T10:06:05.756Z",
"date": "2015-10-31T23:00:00.000Z",
"files": [],
"id": 1,
"metadata": {},
"notes": "note impianto 1",
"parentSubject": {
"code": "soggetto2",
"createdAt": "2015-11-26T10:05:38.765Z",
"id": 1,
"metadata": {},
"notes": "note soggetto 1",
"personalInfo": 1,
"sex": "M",
"tags": null,
"type": 1,
"updatedAt": "2015-11-26T10:05:38.765Z"
}
},
{
"createdAt": "2015-11-26T10:06:36.684Z",
"date": "2015-11-01T23:00:00.000Z",
"files": [],
"id": 2,
"metadata": {},
"notes": "note impianto 2",
"parentSubject": {
"code": "soggetto2",
"createdAt": "2015-11-26T10:05:38.765Z",
"id": 1,
"metadata": {},
"notes": "note soggetto 1",
"personalInfo": 1,
"sex": "M",
"tags": null,
"type": 1,
"updatedAt": "2015-11-26T10:05:38.765Z"
}
}
]'''
#------------------------------------------------------
import json
j = json.loads(data)
results = []
for x in j:
if x["parentSubject"]["code"] == "soggetto1":
results.append(x)
print results
You can do this with list comprehension
results = [ x for x in j if x["parentSubject"]["code"] == "soggetto1" ]
or filter()
results = filter(lambda x:x["parentSubject"]["code"] == "soggetto1", j)