how do i access individual data in json data - json

I am receiving the json data from server as like this
[{
"_id": "588b3731d4428e2ff05ddefa",
"Id": 50,
"Name": "Vishruth",
"Age": 11
}, {
"_id": "588b45df255e323de55ac333",
"Id": 51,
"Name": "Vishruth",
"Age": 11
}]
how do i access the individual data from it
import React from 'react';
import axios from 'axios';
//require('./style.scss');
class Premontessori extends React.Component{
constructor(props){
super(props);
this.state={
post:[]
};
}
componentDidMount(){
let self = this;
axios.get('http://localhost:8080/list')
.then(function(data) {
console.log(data);
self.setState({post:data});
});
}
render(){
return(
<div>
{JSON.stringify(this.state.post.data)}
</div>
);
}
}
export default Premontessori;

Your data is an array. You can simple use map to loop over all elements of the array and then access individual elements of the javascript object using the dot notation or the bracket notation.
class Premontessori extends React.Component{
constructor(props){
super(props);
this.state={
post:[]
};
}
componentDidMount(){
var data = [{
"_id": "588b3731d4428e2ff05ddefa",
"Id": 50,
"Name": "Vishruth",
"Age": 11
}, {
"_id": "588b45df255e323de55ac333",
"Id": 51,
"Name": "Vishruth",
"Age": 11
}]
this.setState({post:data});
}
render(){
return(
<div>
<tbody>
{this.state.post.map(function(item, index) {
return (
<tr>
<td>{item._id}</td>
<td>{item.Id}</td>
<td>{item.Name}</td>
<td>{item.Age}</td>
</tr>
)
})
}</tbody>
</div>
)
}
}
ReactDOM.render(<Premontessori/>, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"><div>

Related

React: Couldn't access properties within data after axios loads. Could it have something to do with params?

I'm coding MERN app and I'm almost done with it. However, I am having problems displaying a single customer page with data.
My App.js
import React from 'react';
import { BrowserRouter as Router, Route } from "react-router-dom";
import CustomersList from "./components/customers-list";
import customerRewards from './components/customerRewards';
import "bootstrap/dist/css/bootstrap.min.css";
function App() {
return (
<Router>
<div className="container">
<div className="header">
<h1>Wow Rewards Program</h1>
</div>
<Route path="/" exact component={CustomersList} />
<Route path='/:id' component={customerRewards} />
</div>
</Router>
);
}
export default App;
My Customer list Component to list customers in main page
import React, { Component } from 'react';
import axios from 'axios';
import CustomerCard from "./customerCard";
class CustomersList extends Component {
constructor(props) {
super(props);
this.state = {
customers: []
}
}
componentDidMount() {
axios.get('http://localhost:5000/customers/')
.then(response => {
this.setState({ customers: response.data });
})
.catch((error) => {
console.log(error);
})
}
render() {
const customers = this.state.customers;
console.log("customerList", customers);
let customersList;
if(!customers) {
customersList = "there is no customer record!";
} else {
customersList = customers.map((customer, k) =>
<CustomerCard customer={customer} key={k} />
);
}
return (
<div>
<h2>Customers List</h2>
<div>
<table>
<thead>
<tr>
<th>Account</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{customersList}
</tbody>
</table>
</div>
</div>
)
}
}
export default CustomersList;
My CustomerCard Component to display each customer
import React from 'react';
import { Link } from 'react-router-dom';
const CustomerCard = (props) => {
const customerCard = props.customer;
return (
<tr>
<td>{customerCard.account}</td>
<td>{customerCard.name}</td>
<td>
<Link to={`/${customerCard.account}`}>view</Link>
</td>
</tr>
)
}
export default CustomerCard;
My Customer Rewards Component to list customer details
import React, { Component } from 'react';
import { Link} from 'react-router-dom';
import '../App.css';
import axios from 'axios';
class customerRewards extends Component {
constructor(props) {
super(props);
this.state = {
customerRewards: {}
};
}
componentDidMount() {
// const { accountId } = this.props.match.params;
console.log("Print id: " + this.props.match.params.id);
axios
.get('http://localhost:5000/customers/'+this.props.match.params.id)
.then(res => {
// console.log("Print-customerRewards-API-response: " + res.data);
this.setState({
customerRewards: res.data
})
})
.catch(err => {
console.log("Error from customerRewards");
})
};
render() {
const customerRewards = this.state.customerRewards;
console.log("customerID", customerRewards);
let CustomerItem = <div>
{customerRewards.purchase_history}
</div>
return (
<div >
<div className="container">
<div className="row">
<div className="col-md-10 m-auto">
<br /> <br />
<Link to="/" className="btn btn-outline-warning float-left">
Back
</Link>
</div>
<br />
<div className="col-md-8 m-auto">
<h1 className="display-4 text-center">Hello {customerRewards.name}</h1>
<p className="lead text-center">
{customerRewards.account}
</p>
<hr /> <br />
</div>
</div>
<div>
{ CustomerItem }
</div>
</div>
</div>
);
}
}
export default customerRewards;
this is my .json sample that I uploaded to MongoDB Atlas:
[
{
"account": "BCE456",
"name": "JohnDoe",
"purchase_history": [
{
"month": "Month01",
"transitions": [
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
}
]
},
{
"month": "Month02",
"transitions": [
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
}
]
}
]
},
{
"account": "ABC123",
"name": "JohnDoe",
"purchase_history": [
{
"month": "Month01",
"transitions": [
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
}
]
},
{
"month": "Month02",
"transitions": [
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
},
{
"date": "01/01/2010",
"purchase": 120
}
]
}
]
}
]
I can see that I was able to download the customer details in console.log("customerID", customerRewards); However, when I tried to access {customerRewards.name}, it won't display the name at all....
What am I missing? Could it have something to do with the params that I'm trying to link to? I tried to link by account instead of _id
Your piece of state, customerRewards is an array when it comes from the API. So even when you declare your initial state with an object {}, customerRewards is always going to be an array with one element in this case. Could be an issue in your API response - I'm not familiar with MongoDB Atlas or if it creates access patterns for you but you should be able to configure to return a singular object with a unique ID rather than an array of 1 element.
For now you could do:
this.setState({
customerRewards: res.data[0] /** could do res.data.shift() as well */
})
I would figure out why you are returning an array if there is only supposed to be one element coming back.
just select the element which name you want to get:
customerRewards[0].name

How to I use object based json data in Angular

//Employee service starts
import {
Injectable
} from "#angular/core";
import {
HttpClient,
HttpErrorResponse
} from "#angular/common/http";
import {
IEmployee
} from "../interfaces/employee";
import {
Observable
} from "rxjs/Observable";
import "rxjs/add/operator/catch";
import "rxjs/add/observable/throw";
#Injectable()
export class EmployeeService {
private _url: string = "assets/data/employee.json";
constructor(private http: HttpClient) {}
getEpmloyees(): Observable < IEmployee[] > {
return this.http.get < IEmployee[] > (this._url).catch(this.errorHandler);
}
errorHandler(error: HttpErrorResponse) {
return Observable.throw(error.message || "Server Error");
}
}
//Employee service end
//-------**********-------*********------**********-------
//Employee component starts
#Component({
selector: "app-employee-details",
templateUrl: "./employee-details.component.html",
styleUrls: ["./employee-details.component.scss"]
})
export class EmployeeDetailsComponent implements OnInit {
public employees = [];
public errorMsg;
constructor(private _employeeService: EmployeeService) {}
ngOnInit() {
this._employeeService.getEpmloyees().subscribe(
data => ((this.employees = data),
error => (this.errorMsg = error)
);
}
}
//Employee component ends
<p>{{errorMsg}}</p>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
<th>Mobile</th>
<th>Action</th>
</tr>
<tbody *ngFor="let employee of employees">
<tr>
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.age}}</td>
<td>{{employee.mob}}</td>
<td><button>Delete</button></td>
</tr>
</tbody>
</table>
<!--Used JSON data
[
{ "id": 1, "name": "sam", "age": 25, "mob": 9996663752 },
{ "id": 2, "name": "adward", "age": 26, "mob": 9124563712 },
{ "id": 3, "name": "yoshi", "age": 27, "mob": 9246663012 },
{ "id": 4, "name": "casie", "age": 29, "mob": 967663012 }
]
-->
<!-- This data through items object I want to use in above code
{
"items": [
{ "id": 1, "name": "harry", "age": 33, "mob": 3476643752 },
{ "id": 2, "name": "jorge", "age": 54, "mob": 7845633712 },
{ "id": 3, "name": "timon", "age": 65, "mob": 9753833012 },
{ "id": 4, "name": "simon", "age": 43, "mob": 8547763012 }
]
}
-->
Above is my Employee service code and employee's component which is working fine and so here also I have shared my JSON data on how I use second JSON data (which is below the first JSON data) and which is based on "items" object so I am in a situation that what changes do I need to get object base JSON data..?
Try with this.employees = data.items
ngOnInit() {
this._employeeService.getEpmloyees().subscribe(
data => ((this.employees = data.items),
error => (this.errorMsg = error)
);
}
}
Try creating a new interface for the Json data you're gonna retrieve.
interface RootObject {
items: IEmployee[];
}
In your service, get this RootObject and return it to your components.
public getEmployees(): RootObject {
return this.http.get<RootObject>(this._url).catch(this.errorHandler);
}
In your Employee Component, retrieve the array of IEmployees from "items" in the RootObject.
ngOnInit() {
this._employeeService.getEmployees().subscribe(
data => ((this.employees = data.items),
error => (this.errorMsg = error)
);
}
}
Be careful, you misspelled Employees in your service function. You might want to enable spell check in your IDE to avoid mistakes ;)

How to use map on json response returned by a REST API with ReactJs

I've a json. The only thing I want is title from the json.
{
"projects": [
{
"id": 1,
"title": "Bike Servicing System",
"language": "JavaFX",
"requires": "JDK 8"
},
{
"id": 2,
"title": "Air Traffic Controller",
"language": "JavaFX",
"requires": "JDK 8"
},
{
"id": 3,
"title": "Program Counter",
"language": "JavaFX",
"requires": "JDK 8"
}
],
"profile": {
"name": "typicode"
}
}
I am using fetch and componentDidMount. I want to do it musing map method to iterate through. Though I don't need <ul> and <li> tags really. I will remove them later. My React code is
import React, { Component } from "react";
class ProjectStack extends Component {
constructor(props) {
super(props);
this.state = {
items: [],
isLoaded: false
};
}
componentDidMount() {
fetch("http://my-json-server.typicode.com/tmtanzeel/json-server/projects/")
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json.projects
});
});
}
render() {
var { isLoaded, items } = this.state;
var i=0;
if (!isLoaded) return <div>Loading...</div>;
return (
<div>
<ul>
{
items.map(item => (
<li key={item[i++].id}>
Projects: {item[i++].title}
</li>
))
}
</ul>
</div>
);
}
}
export default ProjectStack;
Apparently, there is something that I don't know because I am getting this error.
PS: This question is different from mine
The JSON of the URL you are fetching is this:
[
{
"id": 1,
"title": "Bike Servicing System",
"language": "JavaFX",
"requires": "JDK 8"
},
{
"id": 2,
"title": "Air Traffic Controller",
"language": "JavaFX",
"requires": "JDK 8"
},
{
"id": 3,
"title": "Program Counter",
"language": "JavaFX",
"requires": "JDK 8"
},
{
"id": 4,
"title": "Dove Tail",
"language": "JavaFX",
"requires": "JDK 8"
}
]
So for correctly set the data in the state you need to:
componentDidMount() {
fetch("http://my-json-server.typicode.com/tmtanzeel/json-server/projects/")
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json
});
});
}
Besides correcting the error they have already told you related the map loop.
I found 2 mistakes from your code.
The first one is you didn't check the response data.
fetch("http://my-json-server.typicode.com/tmtanzeel/json-server/projects/")
.then(res => res.json())
.then(json => {
console.log(json); // it is an Array not object.
this.setState({
isLoaded: true,
items: json
});
});
And the second is you didn't use the map() properly.
/*{
items.map(item => (
<li key={item[i++].id}>
Projects: {item[i++].title}
</li>
))
}*/
// items has objects, so you should use map() like this.
{
items.map(item => (
<li key={item.id}>
Projects: {item.title}
</li>
))
}
The below code is one way to achieve what you want.
class ProjectStack extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [],
isLoaded: false
};
}
componentDidMount() {
fetch("http://my-json-server.typicode.com/tmtanzeel/json-server/projects/")
.then(res => res.json())
.then(json => {
console.log(json);
this.setState({
isLoaded: true,
items: json
});
});
}
render() {
const {
isLoaded,
items
} = this.state;
console.log(items);
if (!isLoaded) return ( < div > Loading... < /div>);
return ( <
div >
<
ul > {
items.map(item => ( <
li key = {
item.id
} > Projects: {
item.title
} < /li>
))
} <
/ul> <
/div>
);
}
}
ReactDOM.render( <
ProjectStack / > , document.getElementById('root')
)
<script crossorigin src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
<div id="root"></div>

My React navigation is dynamic, warning : You should only render one navigator explicitly in your app

I'm struggling to make my dynamic navigation working in my react-native app.
Here is what I have on my AppNavigation.js :
import {
createDrawerNavigator,
createStackNavigator,
createSwitchNavigator, DrawerItems, SafeAreaView
} from 'react-navigation'
import LoginScreen from '../screens/LoginScreen'
import ProfileScreen from "../screens/ProfileScreen";
import TemplatesScreen from "../screens/TemplatesScreen";
import AuthLoadingScreen from "../screens/AuthLoadingScreen";
import React from "react";
import {Button, Icon} from "native-base";
import {ScrollView} from "react-native";
import NewFilmScreen from "../screens/NewFilmScreen";
import SettingsScreen from "../screens/SettingsScreen";
import LogoutScreen from "../screens/LogoutScreen";
import TemplateWorkflowContainer from "./TemplateWorkflowContainer";
const WorkflowContainer = createStackNavigator(
{
TemplateContainer: {
screen: TemplateWorkflowContainer
}
},
{
headerMode: 'none',
}
);
// drawer stack
const AppNavigation = createDrawerNavigator({
TemplatesScreen: {screen: TemplatesScreen},
NewFilm: {screen: NewFilmScreen},
ProfileScreen: {screen: ProfileScreen},
SettingsScreen: {screen: SettingsScreen},
LogoutScreen: {screen: LogoutScreen}
},
{
drawerBackgroundColor: '#ff4559',
// Default config for all screens
headerMode: 'none',
initialRouteName: 'TemplatesScreen',
contentOptions: {
activeTintColor: '#fff',
inactiveTintColor: '#fff',
itemsContainerStyle: {
marginVertical: 0,
},
itemStyle: {
flexDirection: 'row-reverse',
},
iconContainerStyle: {
opacity: 0.8,
}
},
contentComponent: props =>
<ScrollView>
<SafeAreaView forceInset={{top: 'always', horizontal: 'never'}}>
<Button transparent>
<Icon name='close' style={{fontSize: 40, color: 'white'}} onPress={() => {
props.navigation.closeDrawer()
}}/>
</Button>
<DrawerItems {...props} />
</SafeAreaView>
</ScrollView>
});
const WrapperStack = createStackNavigator({
AppDrawer: AppNavigation,
WorkflowContainer: WorkflowContainer
},
{
headerMode: 'none'
}
);
// Manifest of possible screens, when the user sign in the loginStack will be unmount to never logged out the user with
// the back button
const PrimaryNav = createSwitchNavigator({
AuthLoading: {screen: AuthLoadingScreen},
Auth: {screen: LoginScreen},
App: {screen: WrapperStack}
}, {
initialRouteName: 'AuthLoading'
});
export default PrimaryNav;
My drawer is fine. The problem is on the WorkflowContainer. This is a navigation like this :
import React, { Component } from "react";
import { createStackNavigator } from "react-navigation";
import TemplateWorkflowNavigator from "./TemplateWorkflowNavigator";
export default class TemplateWorkflowContainer extends Component {
constructor(props) {
super(props);
this.state = {
content: null
};
}
generateScreens = data => {
const stack = {};
stack["0"] = {
screen: TemplateWorkflowNavigator,
navigationOptions: () => ({
title: data.title,
gesturesEnabled: true
})
};
for (let i = 0; i < data.scenes.length; i++) {
let screenNumber = data.scenes[i].priority + 1;
stack[screenNumber] = {
screen: TemplateWorkflowNavigator,
navigationOptions: () => ({
title: data.scenes[i].name,
gesturesEnabled: true
})
};
}
return stack;
};
renderStackNavigo = aTemplate => {
const TemplateStackNavigor = createStackNavigator(
this.generateScreens(aTemplate), {headerMode: 'none'}
);
return <TemplateStackNavigor screenProps={aTemplate}/>;
};
render() {
return this.props.navigation.state.params.json && this.renderStackNavigo(this.props.navigation.state.params.json);
}
}
It's dynamic, throught the this.props.navigation.state.params.jsoni got back a JSON like this :
{
"id": 5,
"title": "toto",
"dolly": 74,
"name": "toto",
"conditions": [
{
"name": "Calm",
"desc": "test",
"priority": 0
}
],
"medias": [
{
"path": "a_path_here",
"mobile_path": "a_path_here",
"size": 80851,
"type": "preview"
}
],
"scenes": [
{
"name": "Intro",
"priority": 0,
"conditions": [
{
"name": "smile",
"desc": "test",
"priority": 0
}
],
"medias": [
{
"path": "a_path_here",
"mobile_path": "a_path_here",
"size": 80851,
"type": "preview"
}
],
"elements": [
{
"name": "Name",
"priority": 0,
"type": "text",
}
]
}
]
}
It's working when I call this
this.props.navigation.navigate("TemplateContainer", { json: path });
But I have this warning :
You should only render one navigator explicitly in your app, and other
navigators should by rendered by including them in that navigator.
I tried a lot of things, but I'm so new on react native, nothing worked.
How can I make this navigation works with no warning ? What changes do I have to apply ?
As my assumption, your TemplateWorkflowContainer will look like this
export default class TemplateWorkflowContainer extends Component {
static router = null;
...
renderStackNavigo = aTemplate => {
const TemplateStackNavigor = createStackNavigator(
this.generateScreens(aTemplate), {headerMode: 'none'}
);
TemplateWorkflowContainer.router = TemplateStackNavigor.router;
return <TemplateStackNavigor screenProps={aTemplate}/>;
};
...
}

Import JSON data in to array in REACTJS

So ive been looking at this for a couple hours now trying to get my head around it but I just cant figure it out.
I have a json file located at '/src/data/topbar.json' which i want to include in my topbar-container component which will be used to generate the top menu.
What am I doing wrong here?
topbar.json:
{
"topbarLinks": [
{
"id": 1,
"icon": "header__topbar__list__link__icon glyphicon glyphicon-home",
"text": "home",
"link": "/"
},
{
"id": 2,
"icon": "header__topbar__list__link__icon glyphicon glyphicon-euro",
"text": "Pricing",
"link": "/pricing"
},
{
"id": 3,
"icon": "header__topbar__list__link__icon glyphicon glyphicon-exclamation-sign",
"text": "Help",
"link": "/help"
},
{
"id": 4,
"icon": "header__topbar__list__link__icon glyphicon glyphicon-question-sign",
"text": "FAQ",
"link": "/faq"
},
{
"id": 5,
"icon": "header__topbar__list__link__icon glyphicon glyphicon-edit",
"text": "Register",
"link": "/register"
},
{
"id": 6,
"icon": "header__topbar__list__link__icon glyphicon glyphicon-share",
"text": "Login",
"link": "/login"
}
]
}
topbar-container.js
import React, { Component } from 'react';
import './topbar-container.scss';
import Link from '../topbar-link/topbar-link';
require ('../../data/topbar.json');
class TopbarContainer extends Component {
constructor() {
super();
this.State = {
topbarLinks: []
}
}
componentDidMount() {
fetch('../../data/topbar.json')
.then(results => {
return results.json();
}).then(data => {
let topbarLinks = data.results.map((topbarLinks, key) => {
return (
<Link
key={topbarLinks.id}
text={topbarLinks.text}
icon={topbarLinks.icon}
link={topbarLinks.link}
/>
)
})
})
}
render() {
return (
<div className="container-fluid header__topbar">
<div className="row">
<div className="container">
<ul className="header__topbar__list">
{this.state.topbarLinks}
</ul>
</div>
</div>
</div>
);
}
}
export default TopbarContainer;
You can't fetch a local JSON file, you either have to import it, or setup a webserver that will serve that JSON file
import myJson from '../../data/topbar.json';
Then just map over it and don't forget to setState
componentDidMount() {
let topbarLinks = myJson.topbarLinks.map((topbarLinks, key) => {
return (
<Link
key={topbarLinks.id}
text={topbarLinks.text}
icon={topbarLinks.icon}
link={topbarLinks.link}
/>
)
})
this.setState({topbarLinks: topbarLinks}); // <--
//or just this.setState({ topbarLinks });
}
and as somebody else noted this.state has to be lowercase
topbar.json:
export default {
"topbarLinks": []
}
then you can simply import it without fetch
import data from '../../data/topbar.json'
let topbarLinks = data.results.map((topbarLinks, key) => {
return (
;
I don't think that State should be capitalized in this.State in your constructor.
Change map function to data.topbarLinks.map to access json data.
let topbarLinks = data.topbarLinks.map((topbarLinks, key) => {
return (
<Link
key={topbarLinks.id}
text={topbarLinks.text}
icon={topbarLinks.icon}
link={topbarLinks.link}
/>
)
})
And then set state
this.setState({topbarLinks: topbarLinks});
Change your initial state as this.State to this.state in constructor.