python how to get dictionally object in json? - arrays

I start learning json. I can access title object like this
json_data = results['local_results']
for i in json_data:
if "title" in i:
title = i["title"]
How to accesss latitude object of gps_coordinates, website object of links.
here is my json data.
"local_results": [
{
"position": 1,
"title": "McDonald's",
"place_id": "2142927458143177356",
"lsig": "AB86z5W5r155sIcs3jqfYkm9Y8Fp",
"place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=austin%2C+texas%2C+united+states&lsig=AB86z5W5r155sIcs3jqfYkm9Y8Fp&ludocid=2142927458143177356&q=McDonald%27s&tbm=lcl&token=16c37bb72affc4f2",
"address": "Austin, TX",
"phone": "(512) 442-0412",
"hours": "Open ⋅ Closes 12AM",
"gps_coordinates": {
"latitude": 30.260337999999994,
"longitude": -97.7581347
},
"links": {
"website": "https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks",
"directions": "https://www.google.com/maps/dir//McDonald's,+1209+Barton+Springs+Rd,+Austin,+TX+78704/data=!4m6!4m5!1m1!4e2!1m2!1m1!1s0x8644b51bd54fc423:0x1dbd352b620e0a8c?sa=X&hl=en"
}
},

Json data:
x = {"local_results": [
{
"position": 1,
"title": "McDonald's",
"place_id": "2142927458143177356",
"lsig": "AB86z5W5r155sIcs3jqfYkm9Y8Fp",
"place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=austin%2C+texas%2C+united+states&lsig=AB86z5W5r155sIcs3jqfYkm9Y8Fp&ludocid=2142927458143177356&q=McDonald%27s&tbm=lcl&token=16c37bb72affc4f2",
"address": "Austin, TX",
"phone": "(512) 442-0412",
"hours": "Open ⋅ Closes 12AM",
"gps_coordinates": {
"latitude": 30.260337999999994,
"longitude": -97.7581347
},
"links": {
"website": "https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks",
"directions": "https://www.google.com/maps/dir//McDonald's,+1209+Barton+Springs+Rd,+Austin,+TX+78704/data=!4m6!4m5!1m1!4e2!1m2!1m1!1s0x8644b51bd54fc423:0x1dbd352b620e0a8c?sa=X&hl=en"
}
}]}
Coordinates extraction:
x['local_results'][0]['gps_coordinates']
Ouput:
{'latitude': 30.260337999999994, 'longitude': -97.7581347}
Website extraction:
print(x['local_results'][0]['links']['website'])
output:
https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks

Here is your JSON data:
data = {"local_results": [
{
"position": 1,
"title": "McDonald's",
"place_id": "2142927458143177356",
"lsig": "AB86z5W5r155sIcs3jqfYkm9Y8Fp",
"place_id_search": "https://serpapi.com/search.json?device=desktop&engine=google&gl=us&google_domain=google.com&hl=en&location=austin%2C+texas%2C+united+states&lsig=AB86z5W5r155sIcs3jqfYkm9Y8Fp&ludocid=2142927458143177356&q=McDonald%27s&tbm=lcl&token=16c37bb72affc4f2",
"address": "Austin, TX",
"phone": "(512) 442-0412",
"hours": "Open ⋅ Closes 12AM",
"gps_coordinates": {
"latitude": 30.260337999999994,
"longitude": -97.7581347
},
"links": {
"website": "https://www.mcdonalds.com/us/en-us/location/TX/AUSTIN/1209-BARTON-SPRINGS/4941.html?cid=RF:YXT:GMB::Clicks",
"directions": "https://www.google.com/maps/dir//McDonald's,+1209+Barton+Springs+Rd,+Austin,+TX+78704/data=!4m6!4m5!1m1!4e2!1m2!1m1!1s0x8644b51bd54fc423:0x1dbd352b620e0a8c?sa=X&hl=en"
}
}]}
You enter JSON information into this website (http://jsonviewer.stack.hu/) and you see your data regularly and you can easily access it.
so, here you can access the link and gps_coordinates this way:
print(data['local_results'][0]['gps_coordinates'])
print(data['local_results'][0]['links'])

Related

How to Break json object in a for each

How can one split the below json example by different email in a for each loop? So the end result is two completely separate entities keeping there own objects and arrays.
This result comes from a sql dump but the sql connector alters the output, so I've had to parse that output in a for each loop of its own even though there is no loop it's all in one line.
[
{
"Email": "Steve#gmail.com",
"Name": "Steve Larson",
"ID": "1111",
"Date": "2022-09-12",
"Address": "10 Chicken Place",
"Town": "Gatebody",
"Postcode": "xxx 1xx",
"Data": [
{
"Ref": "34546",
"Transaction_Date": "2018-11-29",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep1"
},
{
"Ref": "34546v2",
"Transaction_Date": "2018-12-24",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep2"
}
]
},
{
"Email": "innis#gmail.com",
"Name": "Innis Blitz",
"ID": "3456",
"Date": "2022-10-12",
"Address": "33 Snake Road",
"Town": "March",
"Postcode": "cxc 3cd",
"Data": [
{
"Ref": "078776701",
"Transaction_Date": "2021-08-27",
"Amount": 984.68,
"Balance": 984.68,
"Notes": "Yes please"
},
{
"Ref": "078776701v2",
"Transaction_Date": "2021-08-27",
"Amount": 98422.6,
"Balance": 98432.6,
"Notes": "Please not now"
}
]
}
]
Would like to do a for each so it splits the above like so. The idea then for my to load the result of the for each into a template that takes arrays.
{
"Email": "Steve#gmail.com",
"Name": "Steve Larson",
"ID": "1111",
"Date": "2022-09-12",
"Address": "10 Chicken Place",
"Town": "Gatebody",
"Postcode": "xxx 1xx",
"Data": [
{
"Ref": "34546",
"Transaction_Date": "2018-11-29",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep1"
},
{
"Ref": "34546v2",
"Transaction_Date": "2018-12-24",
"Amount": 27.76,
"Balance": 27.76,
"Notes": "Beep2"
}
]
}
{
"Email": "innis#gmail.com",
"Name": "Innis Blitz",
"ID": "3456",
"Date": "2022-10-12",
"Address": "33 Snake Road",
"Town": "March",
"Postcode": "cxc 3cd",
"Data": [
{
"Ref": "078776701",
"Transaction_Date": "2021-08-27",
"Amount": 984.68,
"Balance": 984.68,
"Notes": "Yes please"
},
{
"Ref": "078776701v2",
"Transaction_Date": "2021-08-27",
"Amount": 98422.6,
"Balance": 98432.6,
"Notes": "Please not now"
}
]
}
I have reproduced in my environment and got expected results and I followed below process:
I have initialized your input as below:
Then i added for each control and send the output of previous step as input as inside of foreach loop, i have added send email action.
Outputs:
I have got tow jsons in two seperate emails as above.

How to get certain keys from the array of objects and put them in state?

I am training to do requests to server with json.placeholder. The result of request is the array of objects with many keys.
For example the array of objects
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna#melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
},
and so on ...
But I don't need all these keys. Only to take some of them, for example name, username and id and put it in my State.
How to do that properly?
TL;DR
response = [ { "id": 1, "name": "Leanne Graham", ...}, {...}, ...]
cleanResponse = response.map(user => return {name: user.name, contact: user.email})
Here, with .map we return an object that has two property : name and contact.
We set name to user.name and contact to user.email
Anwser
I don't know much about ReactJS, but your anwser only require basic javascript :
Array.Map
You can use .map on an array to convert its elements :
nameArray= [ "George", "Alice", "Portevent" ]
presentationArray = nameArray.map(name => "My name is " + name)
// presentationArray = [ "My name is George", "My name is Alice", "My name is Portevent" ]
nameArray.map will iterate on each element. name will be equal to "George", then "Alice" etc...
Foreach element, it will be replaced with "My name is " + name (remember, name will have each different value).
Note : .map doesn't change the initial array, it just create another array (so we save it inside presentationArray
A more advanced use of map :
numbers = [1, 2, 3]
function tenToPower(n) {
return 10 ** n
}
numbers.map(number => tenToPower) // [10, 100, 1000]
numbers.map(number => {
if (number < 3) return "Failed"
else return "Valid"
}) // ["Failed", "Failed", "Valid"]
const item = data.map((user) => {
if(user.username === 'Bret'){
setName(user.name);
setUserName(user.username);
setId(user.id)
}
})

How we can show collection data in array

When I am using get api and get the data in json format
{
"uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"name": "AMan",
"dispute_location": "Hyder",
"max_dispute_value": "200",
"min_dispute_value": "100",
"state": "U.p",
"district": "morene",
"calendar_id": "3",
"description": null,
"subject_matters": [
{
"id": 1,
"name": "A",
"created_at": "2020-08-14T12:24:52.000000Z",
"updated_at": "2020-08-14T12:24:52.000000Z",
"pivot": {
"court_uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"subject_matter_id": 1
}
},
{
"id": 2,
"name": "B",
"created_at": "2020-08-14T12:24:57.000000Z",
"updated_at": "2020-08-14T12:24:57.000000Z",
"pivot": {
"court_uuid": "46d00217-6e35-485c-ac20-c204a8a24a68",
"subject_matter_id": 2
}
}
]
}
but i want subject_matters data in this format
"subject_matters": [1,2]
Api resource (court resources)
You can use ->pluck('id'); in your resource.
return [
'subject_matters_id' => $this->subjectMatters->pluck('id')
]
It should return an array of subject matter IDs.

How to create a JSON structure in ReactJS

I have a use-case to create a JSON structure in React in order to POST an API request. The JSON structure body contains objects and arrays.
Please let me know how to create it in ReactJS.
Below is the sample JSON structure that needs to be created using ReactJS:-
{
"transactionAmount": {
"currency": "INR",
"value": 1220.38
},
"transactionDate": "2020-05-18T00:00:00Z",
"tripData": {
"agencyBooked": false,
"legs": [
{
"endLocation": {
"countryCode": "IN",
"city": "Delhi",
"name": "Indira Gandhi International"
},
"startDate": "2020-05-22",
"startTime": "08:00",
"returnLeg": false,
"startLocation": {
"countryCode": "US",
"city": "San Francisco",
"name": "San Francisco International"
},
"endTime": "21:00",
"endDate": "2020-05-22",
"startLocationDetail": "none"
},
{
"endLocation": {
"countryCode": "US",
"city": "San Francisco",
"name": "San Francisco International"
},
"returnLeg": true,
"startDate": "2020-05-24",
"startLocation": {
"countryCode": "IN",
"city": "Delhi",
"name": "Indira Gandhi International"
},
"startTime": "17:00"
}
],
"segmentType": {
"category": "REQ_SEG_AIRFR",
"code": "AIRFR"
},
"selfBooked": false,
"tripType": "ROUND_TRIP"
}
}
You can simply make a JSON format like this.
const dummyObject = {
name: 'Dummy',
age: 22,
about: {
hobbies: ['soccer']
},
works: true
}

How to fetch array of array data in angular 2?

How to fetch array of array data in angular 2.
My json data is as follows,
[[{
"pk_emp_id":5,
"tenant_id":"Zone1",
"location_id":1,
"emp_number":"sk44",
"prefix":"",
"first_name":"qqqqq",
"middle_name":"www",
"last_name":"eeee",
"display_name":"qqqq",
"full_name":"qqq qqqq",
"email":"qqqq#gmail.com",
"gender":"Female",
"emp_type_id":2,
"date_of_hire":191000,
"date_of_birth":null,
"manager_id":7,
"phone_number":"9877654",
"position":"SE",
"responsibility":"",
"notes":"",
"contracted":"0",
"street":"vidyanagar",
"state":"Karnataka",
"city":"hubli",
"zip_code":"9898",
"dob":-19800000,
"ssn":"",
"deleted":0
},{
"pk_empt_id":2,
"empt_tenant_id":"2",
"***empt_name***":"temporary",
"deleted":0
}]]
How can I fetch empt_name in datatable(row data)?
let dummyArr = [
[{
"pk_emp_id": 5,
"tenant_id": "Zone1",
"location_id": 1,
"emp_number": "sk44",
"prefix": "",
"first_name": "qqqqq",
"middle_name": "www",
"last_name": "eeee",
"display_name": "qqqq",
"full_name": "qqq qqqq",
"email": "qqqq#gmail.com",
"gender": "Female",
"emp_type_id": 2,
"date_of_hire": 191000,
"date_of_birth": null,
"manager_id": 7,
"phone_number": "9877654",
"position": "SE",
"responsibility": "",
"notes": "",
"contracted": "0",
"street": "vidyanagar",
"state": "Karnataka",
"city": "hubli",
"zip_code": "9898",
"dob": -19800000,
"ssn": "",
"deleted": 0
}, {
"pk_empt_id": 2,
"empt_tenant_id": "2",
"***empt_name***": "temporary",
"deleted": 0
}]
];
dummyArr.forEach(element =>{
element.forEach(element2=>{
console.log(element2)
})
});
I believe you have exposed a GET method in your web api
In Angular using HTTP service
this.http.get("").subscribe(result => {
conosle.log(result[1].empt_name) });
You can take it in an array and read it like this:
x = [
[{
"pk_emp_id": 5,
"tenant_id": "Zone1",
"location_id": 1,
"emp_number": "sk44",
"prefix": "",
"first_name": "qqqqq",
"middle_name": "www",
"last_name": "eeee",
"display_name": "qqqq",
"full_name": "qqq qqqq",
"email": "qqqq#gmail.com",
"gender": "Female",
"emp_type_id": 2,
"date_of_hire": 191000,
"date_of_birth": null,
"manager_id": 7,
"phone_number": "9877654",
"position": "SE",
"responsibility": "",
"notes": "",
"contracted": "0",
"street": "vidyanagar",
"state": "Karnataka",
"city": "hubli",
"zip_code": "9898",
"dob": -19800000,
"ssn": "",
"deleted": 0
}, {
"pk_empt_id": 2,
"empt_tenant_id": "2",
"***empt_name***": "temporary",
"deleted": 0
}]
]
$('#a').text(x[0][1]['***empt_name***'])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<span id='a'>sadsad </span>
This is just Javascript object manipulation.
You can do something like :
outerArray.forEach(innerArray =>{
if(innerArray.length){
innerArray.forEach(element =>{
//do somthing here with element.empt_name
console.log(element.empt_name);
});
}
})

Resources