Acess Nested array objects in Json file from react - reactjs

This is my events.json file
events.json
{
"data":
[
{
"slug": 11305,
"slugforurl": "11305-cretech-london",
"summary": "Cretech London ",
"start": {
"minutetimezone": "00"
},
"end": {
"minutetimezone": "00"
},
"areas": [
{
"slug": 27,
"title": "London"
}
],
"country": { "title": "United Kingdom" }
},
{
"slug": 11439,
"slugforurl": "11439-pydata-global-2021-global-online-conference",
"summary": "PyData Global 2021 (GLOBAL ONLINE CONFERENCE)",
"start": {
"minutetimezone": "30"
},
"end": {
"minutetimezone": "30"
},
"areas": [
{
"slug": 62,
"title": "Edinburgh"
}
],
"country": { "title": "United Kingdom" }
},
{
"slug": 11450,
"slugforurl": "11450-stormid-coderdojo-thursday-october-28th",
"summary": "StormID CoderDojo - Thursday October 28th",
"start": {
"minutetimezone": "30"
},
"end": {
"minutetimezone": "00"
},
"venue": {
"slug": 735,
"title": "Storm ID",
"description": "Storm ID Offices",
"address": "43 Constitution Street",
"addresscode": "EH6 7BS",
"lat": "55.9759",
"lng": "-3.1665"
},
"areas": [
{
"slug": 62,
"title": "Edinburgh"
}
],
"country": { "title": "United Kingdom" }
},
{
"slug": 11479,
"slugforurl": "11479-geek-brekky",
"summary": "Geek Brekky",
"start": {
"minutetimezone": "00"
},
"end": {
"minutetimezone": "30"
},
"venue": {
"slug": 380,
"title": "Tamper Sellers Wheel",
"description": "",
"address": "Arundel Street",
"addresscode": "S1 2N",
"lat": null,
"lng": null
},
"areas": [
{
"slug": 40,
"title": "Sheffield"
}
],
"country": { "title": "United Kingdom" }
}```
This is weatherEvents.js file
**WeatherEvents.js**
```import React, { Component } from 'react'
import events from './events.json'
export class WeatherEvents extends Component {
render() {
return (
<div>
<h3>Events with Weather Forecast</h3>
<table>
<thead>
<tr>
<th>#</th>
<th>Name of the Event</th>
<th>City</th>
<th>Weather</th>
</tr>
</thead>
<tbody>
{events.data.map((eve) =>
<tr>
<td>{eve.slug}</td>
<td>{eve.summary}</td>
<td>{eve.areas.map((city,index) =>
<p key={ index}>{ city.title}</p>
)}
</td>
</tr>
)
}
</tbody>
</table>
</div>
)
}
}
Hi,
In the event.json file it shows my json values and in the WeatherEvents.js file it shows the react code. I want to access the title in areas in the events.json file. even I used map() it gives an error. Initially I used map() function to get the inner data and again used map() function to access 'title' inside the areas, for normal nested can used event.country.title it is working properly, but for the areas as it is an array can't access the data. Even though I used map() function it gives tghe following error. Can anyone give me a solution for this.
TypeError: Cannot read properties of undefined (reading 'map')
Thanks

Make sure your JSON data is valid.
You can check the here example with data rendering.
const events = {
"data":
[
{
"slug": 11305,
"slugforurl": "11305-cretech-london",
"summary": "Cretech London ",
"start": {
"minutetimezone": "00"
},
"end": {
"minutetimezone": "00"
},
"areas": [
{
"slug": 27,
"title": "London"
}
],
"country": { "title": "United Kingdom" }
},
{
"slug": 11439,
"slugforurl": "11439-pydata-global-2021-global-online-conference",
"summary": "PyData Global 2021 (GLOBAL ONLINE CONFERENCE)",
"start": {
"minutetimezone": "30"
},
"end": {
"minutetimezone": "30"
},
"areas": [
{
"slug": 62,
"title": "Edinburgh"
}
],
"country": { "title": "United Kingdom" }
},
{
"slug": 11450,
"slugforurl": "11450-stormid-coderdojo-thursday-october-28th",
"summary": "StormID CoderDojo - Thursday October 28th",
"start": {
"minutetimezone": "30"
},
"end": {
"minutetimezone": "00"
},
"venue": {
"slug": 735,
"title": "Storm ID",
"description": "Storm ID Offices",
"address": "43 Constitution Street",
"addresscode": "EH6 7BS",
"lat": "55.9759",
"lng": "-3.1665"
},
"areas": [
{
"slug": 62,
"title": "Edinburgh"
}
],
"country": { "title": "United Kingdom" }
},
{
"slug": 11479,
"slugforurl": "11479-geek-brekky",
"summary": "Geek Brekky",
"start": {
"minutetimezone": "00"
},
"end": {
"minutetimezone": "30"
},
"venue": {
"slug": 380,
"title": "Tamper Sellers Wheel",
"description": "",
"address": "Arundel Street",
"addresscode": "S1 2N",
"lat": null,
"lng": null
},
"areas": [
{
"slug": 40,
"title": "Sheffield"
}
],
"country": { "title": "United Kingdom" }
}
]
}
class WeatherEvents extends React.Component {
render() {
return (
<div>
<h3>Events with Weather Forecast</h3>
<table border="1">
<thead>
<tr>
<th>#</th>
<th>Name of the Event</th>
<th>City</th>
</tr>
</thead>
<tbody border="1">
{events.data.map((eve) =>
<tr>
<td>{eve.slug}</td>
<td>{eve.summary}</td>
<td>{eve.areas.map((city,index) =>
<p key={ index}>{ city.title}</p>
)}
</td>
</tr>
)
}
</tbody>
</table>
</div>
)
}
}
ReactDOM.render(
<WeatherEvents />,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Related

search on json using filter in Vuejs?

I want to search in this array of json based on
track_order
I get this result from server and set in orders variable:
"records": [
{
"phone": "09********8",
"fullName": "******",
"records": [
{
"status": "processing",
"details": "***",
"cost": "1500000",
"date": "۹۹/۰۳/۰۷ ۱۰:۱۴",
"track_order": ""
},
{
"status": "send",
"details": "***",
"cost": "2000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۳۹",
"track_order": "******"
},
{
"status": "cancel",
"details": "******",
"cost": "2000000000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۱۴",
"track_order": ""
},
{
"status": "proccing",
"details": "******",
"cost": "2000000000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۱۴",
"track_order": ""
}
]
},
{
"phone": "093*******8",
"fullName": "*******",
"records": [
{
"status": "send",
"details": "********",
"cost": "2000000000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۴۰",
"track_order": "*************"
}
]
},
{
"phone": "09*********1",
"fullName": "*********",
"records": []
}
]
this is my vue.js code for search I want to filter orders in getOrders and return it:
data(){
return {
orders:[],
search:""
}
}
computed:{
getOrders(){
if(this.search){
for(let i=0;i<this.orders.length;i++){
return this.orders[i].records.filter(record=>{
return this.search.toLowerCase().split(' ').every(v=> record.track_order.toLowerCase().includes(v));
})
}
}else{
return this.orders;
}
}
}
and I set
getOrders
in Table like this:
<tbody v-for="(item,index) in getOrders" :key="index">
<tr v-for="(record,index1) in item.records" :key="index1">
<td >{{item.fullName}}</td>
<td >{{record.details}}</td>
<td>{{record.cost}}</td>
<td >{{record.date}}</td>
<td >{{record.status}}</td>
<td >{{record.track_order}}</td>
</tbody>
But it doesn't work.
And by entering the word to search, the table does not change
What is wrong?
Can you Help me?
new Vue({
el: '#app',
template: `
<div>
<input v-model="search"><button v-if="search" type="button" #click="setDefault">default</button>
<table>
<tbody v-for="(item,index) in getOrders" :key="index">
<tr v-for="(record,index1) in item.records" :key="index1">
<td >{{item.fullName}}</td>
<td >{{record.details}}</td>
<td>{{record.cost}}</td>
<td >{{record.date}}</td>
<td >{{record.status}}</td>
<td >{{record.track_order}}</td>
</tr>
</tbody>
</table>
</div>
`,
data() {
return {
search: "",
records: [{
"phone": "09********8",
"fullName": "******",
"records": [{
"status": "processing",
"details": "***",
"cost": "1500000",
"date": "۹۹/۰۳/۰۷ ۱۰:۱۴",
"track_order": "aloha"
},
{
"status": "send",
"details": "***",
"cost": "2000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۳۹",
"track_order": "******"
},
{
"status": "cancel",
"details": "******",
"cost": "2000000000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۱۴",
"track_order": ""
},
{
"status": "proccing",
"details": "******",
"cost": "2000000000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۱۴",
"track_order": "---"
}
]
},
{
"phone": "093*******8",
"fullName": "*******",
"records": [{
"status": "send",
"details": "********",
"cost": "2000000000000",
"date": "۹۹/۰۳/۰۷ ۱۰:۴۰",
"track_order": "*************"
}]
},
{
"phone": "09*********1",
"fullName": "*********",
"records": []
}
],
};
},
computed: {
getOrders() {
if (!this.search) {
return this.records;
}
return this.getOrdersFiltered;
},
getOrdersFiltered() {
const RECORDS = [...this.records];
const RECORD_NEW = [];
RECORDS.forEach(item => {
const RECORDS_CHILD = item.records.filter(record => {
return record.track_order.toLowerCase().indexOf(this.search) !== -1;
});
if (RECORDS_CHILD.length) {
item.records = RECORDS_CHILD;
RECORD_NEW.push(item)
}
});
return RECORD_NEW;
},
},
methods: {
setDefault() {
this.search = "";
},
},
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app"></div>

retrieving array from json as a table in Kusto

I am trying to retrieve all the rows from a Json array. The json is similar to the one shown below.
{
"messageId": "123",
"fileName": "abc.json",
"payload": {
"routeStatus": "FINAL",
"activities": [
{
"durationSeconds": 1800,
"location": {
"longitude": 151.2603,
"latitude": -33.7644
},
"type": "DEPART",
"slot": {
"start": "2020-04-14T19:05:00.0000000Z",
"cost": null,
"end": "2020-04-15T03:30:00.0000000Z"
}
},
{
"durationSeconds": 1100,
"type": "DRIVE"
},
{
"durationSeconds": 360,
"location": {
"longitude": 151.21814,
"latitude": -33.756319
},
"type": "SERVICE",
"slot": {
"start": "2020-04-14T20:00:00.0000000Z",
"cost": null,
"end": "2020-04-15T00:45:00.0000000Z"
}
},
{
"durationSeconds": 164,
"type": "DRIVE"
}
],
"truck": "XYZ"
}
}
I would like to get all the attributes under the activities in a table as I would need to filter and join to other tables. I am only able to retrieve one row from the array. Any pointers would be helpful.
you can use mv-expand or mv-apply.
for example:
print d = dynamic({
"messageId": "123",
"fileName": "abc.json",
"payload": {
"routeStatus": "FINAL",
"activities": [
{
"durationSeconds": 1800,
"location": {
"longitude": 151.2603,
"latitude": -33.7644
},
"type": "DEPART",
"slot": {
"start": "2020-04-14T19:05:00.0000000Z",
"cost": null,
"end": "2020-04-15T03:30:00.0000000Z"
}
},
{
"durationSeconds": 1100,
"type": "DRIVE"
},
{
"durationSeconds": 360,
"location": {
"longitude": 151.21814,
"latitude": -33.756319
},
"type": "SERVICE",
"slot": {
"start": "2020-04-14T20:00:00.0000000Z",
"cost": null,
"end": "2020-04-15T00:45:00.0000000Z"
}
},
{
"durationSeconds": 164,
"type": "DRIVE"
}
],
"truck": "XYZ"
}
})
| mv-expand d.payload.activities
| project durationSeconds = tolong(d_payload_activities.durationSeconds), type = tostring(d_payload_activities.type)

CategoryComponent.html:53 ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed

I have angular 8 frontends and node js is a backend
I fetched multiple table results in a single function with sequelize package.
below I mention the result JSON from the backend...
{
"filtergroups": [
{
"id": 1,
"name": "Ram",
"subcatId": "1",
"childcatId": 1,
"createdAt": "2019-11-13T00:00:00.000Z",
"updatedAt": "2019-11-13T00:00:00.000Z",
"filters": [
{
"id": 1,
"name": "2 GB",
"filterGroupId": "1",
"productId": "1",
"createdAt": "2019-11-19T00:00:00.000Z",
"updatedAt": "2019-11-27T00:00:00.000Z"
}
]
},
{
"id": 2,
"name": "Internal Storage",
"subcatId": "1",
"childcatId": 1,
"createdAt": "2019-11-05T00:00:00.000Z",
"updatedAt": "2019-11-24T00:00:00.000Z",
"filters": [
{
"id": 2,
"name": "8 GB",
"filterGroupId": "2",
"productId": "1",
"createdAt": "2019-11-20T00:00:00.000Z",
"updatedAt": "2019-11-20T00:00:00.000Z"
}
]
}
],
"products": [
{
"id": 1,
"name": "A7",
"childcatId": "1",
"subcatId": "1",
"price": 18000,
"mrp": 21000,
"discription": "sfhshsfhsf",
"image": "http://bestjquery.com/tutorial/product-grid/demo3/images/img-7.jpeg",
"createdAt": "2019-11-13T00:00:00.000Z",
"updatedAt": "2019-11-14T02:00:00.000Z"
},
{
"id": 2,
"name": "A8",
"childcatId": "1",
"subcatId": "1",
"price": 22000,
"mrp": 25000,
"discription": "7578",
"image": "http://bestjquery.com/tutorial/product-grid/demo3/images/img-8.jpeg",
"createdAt": "2019-11-20T00:00:00.000Z",
"updatedAt": "2019-11-21T00:00:00.000Z"
},
{
"id": 3,
"name": "Woodland",
"childcatId": "2",
"subcatId": "1",
"price": 2500,
"mrp": 2800,
"discription": "7575",
"image": "http://bestjquery.com/tutorial/product-grid/demo3/images/img-7.jpeg",
"createdAt": "2019-11-20T00:00:00.000Z",
"updatedAt": "2019-11-13T00:00:00.000Z"
}
]
}
This JSON I am getting an error in display section like
CategoryComponent.html:53 ERROR Error: Error trying to diff '[object
Object]'. Only arrays and iterables are allowed
service.ts
getAllProduct(): Observable<Filter_group[]>{
let params_url = new HttpParams().set('fil_outlet',this.name);
console.log('new my ss--',this.name);
return this.http.get<Filter_group[]>("http://localhost:4500/mobile_store");
};
my component.ts
this.route.queryParams.subscribe(params => {
this.subcat = params ['=='];
this.childcat = params ['fil_outlet'];
if (this.subcat){
this.categoryService.getAllProduct().subscribe((filter_groups: Filter_group[]) => {
this.filter_groups = filter_groups;
console.log('new Query Params:', filter_groups);
});
}
});
Error its
component.html
<mat-accordion multi="true" *ngFor="let filter_group of filter_groups">
<mat-expansion-panel>
<mat-expansion-panel-header >
{{filter_group.name}}
</mat-expansion-panel-header>
<p >name</p>
</mat-expansion-panel>
</mat-accordion> <!-- filter-group .// -->
<div class="col-md-3 col-sm-6" *ngFor="let product of products">
<div class="product-grid2">
<div class="product-image2">
<a routerLink="/categories">
<img class="pic-1" src="{{product.image}}">
<img class="pic-2" src="{{product.image}}">
</a>
<ul class="social">
<li><a routerLink="/products" data-tip="Quick View"><i class="fa fa-eye"></i></a></li>
<li><i class="fa fa-shopping-bag"></i></li>
</ul>
<span class="product-discount-label">40%</span>
<a class="add-to-cart" href="">Add to cart</a>
</div>
<div class="product-content">
<h3 class="title"><a routerLink="/categories">{{product.name}}</a></h3>
<div class="price">
{{product.price}}
<span>{{product.mrp}}</span>
</div>
You are taking up the full object which has both filter_groups and products and trying to loop it. Angular loops through arrays not objects.
You need to change your code in your subscription
this.categoryService.getAllProduct().subscribe((data: Filter_group) => {
this.filter_groups = data.filtergroups;
this.products = data.products;
console.log('filter group:', filter_groups );
console.log('products:', products);
});

Filtering JSON data in a list

I am new to working with JSON data. I am having difficulty on how to return back bicycles that have a color of "Blue". Using JSONPath.
The following example JSON file.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
"bicycle": [
{
"price": 19.95
"color": [
"red"
],
},
{
"price": 20.99
"color": [
"blue",
"Green"
],
},
]
}
}
I have tried to use the following filter but it doesn't work.
$.store.bicycle[?(#.color=='blue')]
Any ideas to how to get this to work and only return the price of bicycles that are Blue?
Any information is greatly welcomed.
The JSON Data is
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}],
"bicycle": [
{
"price": 19.95,
"color": [
"red"
]
},
{
"price": 20.99,
"color": [
"blue",
"Green"
]
}
]
}
}
The JSONPath is
$.store.bicycle[?(#.color.indexOf('blue') != -1)]

AngularJS ng-repeat display json

I'm having the hardest time figuring out how to display the following JSON file in my Angularjs repeat.
for the following JSON results, I thought I could simply display the title in an ng-repeat with the following:
<div ng-repeat="x in results">
{{x.data[0].title}}
</div>
But I'm not seeing results.
Here is the JSON:
{
"data": [
{
"id": 1,
"title": "Temp Title",
"description": "Temp Description",
"created_at": {
"date": "2016-03-15 07:10:17.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2016-03-15 07:10:17.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"user": {
"data": {
"id": 29,
"displayName": "chris.nakea",
"email": "chris.nakea#freshconsulting.com",
"join_date": 1458025279,
"profile": {
"data": {
"id": 29,
"displayName": "chris.nakea",
"avatar": null,
"firstName": null,
"lastName": null,
"bio": null,
"city": null,
"zipcode": null,
"state": null,
"country": null,
"latitude": null,
"longitude": null,
"avatars": {
"data": [
{
"id": "default_avatar.png",
"filename": "default_avatar.png",
"url": "https://cdn.band.dev/common/images/common/default_avatar.png",
"created_at": {
"date": "2016-03-15 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"images": {
"small": "https://cdn.band.dev/common/images/common/default_avatar_small.png",
"medium": "https://cdn.band.dev/common/images/common/default_avatar_medium.png",
"large": "https://cdn.band.dev/common/images/common/default_avatar_large.png"
}
}
]
},
"coverPhotos": {
"data": []
}
}
}
}
},
"category": {
"data": {
"id": 2,
"name": "Staff / Events",
"description": "Staff / Events",
"colorCode": "#242156",
"iconName": "icon-staff-events",
"iconCharacterCode": "c108"
}
},
"attachments": {
"data": [
{
"id": "1d3f96e2286c27ee599c9e49a0c33da0",
"filename": "man.jpg",
"url": "https://api.band.dev/v1/file/1d3f96e2286c27ee599c9e49a0c33da0",
"created_at": {
"date": "2016-03-15 07:10:17.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"images": {
"small": "https://api.band.dev/v1/file/50af58b3d52d8629e9f5c4d0dcdd5181",
"medium": "https://api.band.dev/v1/file/51535d38f7b3cd82313eac2414059d83",
"large": "https://api.band.dev/v1/file/a7be1dada18e4041cf48aea377cafa29"
}
}
]
}
},
{
"id": 2,
"title": "Temp Title",
"description": "Temp Description",
"created_at": {
"date": "2016-03-15 07:12:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2016-03-15 07:12:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"user": {
"data": {
"id": 29,
"displayName": "chris.nakea",
"email": "chris.nakea#freshconsulting.com",
"join_date": 1458025279,
"profile": {
"data": {
"id": 29,
"displayName": "chris.nakea",
"avatar": null,
"firstName": null,
"lastName": null,
"bio": null,
"city": null,
"zipcode": null,
"state": null,
"country": null,
"latitude": null,
"longitude": null,
"avatars": {
"data": [
{
"id": "default_avatar.png",
"filename": "default_avatar.png",
"url": "https://cdn.band.dev/common/images/common/default_avatar.png",
"created_at": {
"date": "2016-03-15 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"images": {
"small": "https://cdn.band.dev/common/images/common/default_avatar_small.png",
"medium": "https://cdn.band.dev/common/images/common/default_avatar_medium.png",
"large": "https://cdn.band.dev/common/images/common/default_avatar_large.png"
}
}
]
},
"coverPhotos": {
"data": []
}
}
}
}
},
"category": {
"data": {
"id": 2,
"name": "Staff / Events",
"description": "Staff / Events",
"colorCode": "#242156",
"iconName": "icon-staff-events",
"iconCharacterCode": "c108"
}
},
"attachments": {
"data": [
{
"id": "a93cf8df7b60686e7ca6884d0ce353c8",
"filename": "man2.jpg",
"url": "https://api.band.dev/v1/file/a93cf8df7b60686e7ca6884d0ce353c8",
"created_at": {
"date": "2016-03-15 07:12:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"images": {
"small": "https://api.band.dev/v1/file/cd04551395a355f4792fb85833156741",
"medium": "https://api.band.dev/v1/file/4ff543cd8f5055bfecd703dedaee6d87",
"large": "https://api.band.dev/v1/file/5cdd9a0c3650228e0b93f9c6cd1404df"
}
}
]
}
}
]
}
You can just remove the datap[0] part and get the output
<div ng-repeat="x in results.data">
{{x.title}}
</div>
Output:
Temp Title
Temp Title
if you want to filter then you can do it by
<div ng-repeat="x in results.data | filter: { id: '1' }">
{{x.title}}
</div>
Output:
Temp Title
<div ng-repeat="item in data">{{item.title}}</div>
And in your controller, bind the json to the scope.
$scope.data = jsonData.data;
Here's a fiddle for you - jsFiddle
<div ng-repeat="x in results.data">
{{x.title}}
</div>
https://jsfiddle.net/nvqf8oo7/6/
Thank you all for responding. I finally figured out that the reason I wasn't seeing anything was because I am using ui.bootstrap's modal and I was out of scope.
I resolved this by moving the ng-repeat out of the modal, but I could have also tried to work with the modal scope itself.

Resources