How to access json file in Angular4 - json

I want to acccess data.json file in myservice.service.ts .
Can anyone suggest me how to achieve it?
Directory structure
myserivce.service.ts file
Getting error like this

Use URL like data.json and Put the data.json file in your src/ directory. It will work fine.
OR
Use URL like assets/data.json and Put the data.json file in your src/assets/ directory.

Two things.
The data.json file has to exist on the server hosting the web application.
The URL you provide $http is not relative to the service or component that is executing the request (or request chain). You have to specify the absolute url which would be
private _url = '/src/app/data.json';

Related

Unable to update local json file in React native app

My React Native app file structure looks like this:
/assests
/json
example.json
/components
view.js
I need to take input from the user in the view.js component and store it in the example.json file for future use. I tried using react-native-fs but could not figure out how to use it to get the absolute path of example.json and the relative path kept on throwing errors. If there is some other way to do this, it would be most appreciated.
Thanks in advance for the help!!!!
Found out what the problem was. I was trying to update files that are in the app bundle and forgot that they are read-only. So I copied the files from the app bundle to the document directory and is accessible from there. I can edit and delete from there.

env variable in JSON file in public folder of CRA

I have this config.json file located in my public folder, in which I need to put some params different in development and in production modes. I fetch it when my app loads (fetch('config.json').then()).
Is there a way that my config.json file looks like that :
{
"url": "%REACT_APP_URL%"
}
Of course, the code above doesn't work : is there another solution ?
If you are using create-react-app, you don't need to use a config file. Simple create a .env file in the root directory and add your environment variables:
URL=REACT_APP_URL
In your app, simple do something like
const url = process.env.REACT_APP_URL
Check out my post https://medium.com/#selom/nice-article-a446406f4447

Unable to read json files in angular 5?

Hi I am developing web application in Angular 5. I am trying to read json file using http call as below. I am getting 404 error. Below is my service.ts file. Json file also remains in same folder so path may be correct.
public GetNodes() {
return this.http.get('./json1.json');
}
Below is my component code.
ngOnInit() {
this.nodeservice.GetNodes().subscribe(data =>
console.log(data)
);
I have done one work around and found my static files supposed to be in assets folder. I moved my json1.json to assets folder and I tried to access. Still I am getting same 404 error. Below is the full path where my json file kept
Projectname\ClientApp\app\components\Utilities
I am using ./json1.json because my service and json files are in the same folder utilities.
Can someone help me to figure it out the issue? Any help would be appreciated. Thank you.
To access any asset data like image or file, you need to put them in assets folder and access it from there as angular loads all the public data from assets folder so any asset will not be accessible from component's folder.
For file './json1.json' the URL will be HOST_Name/json1.json and it won't be there so better to put all files in assets folder and provide path for assets folder.
You can also change assets configuration in "angular.json" to point to your component's folder to access that json file.

Can't reference json file in root

I created a new Azure WebJobs project which is a console app. I placed a settings.json file in the root and I'm trying to access it using the following code but I keep getting an error that says it cannot locate the file. I think it's looking for it under Debug folder but I don't want to move the file there. How do I reference that file?
var config = new Configuration();
config.AddJsonFile("settings.json");
I tried "~/settings.json" but that didn't work either.
You need to identify if it's a deployment or runtime issue, per this article.
Make sure that your file is in fact getting deployed:
In VS, check that it has Copy to Output directory set to Copy if Newer
Use Kudu Console to look at the relevant WebJob folder under D:\home\site\wwwroot\App_Data\jobs\... and make sure that the json file made it to there next to the exe.
You can try to add your json file into your WebJob project's Resources as shown:
Remember to set the file type as Text and encoding to UTF-8.
In your code, you can easily access your json file as string as below:
// The Resources property depends on your actual file name being referenced
var settingsJson = Resources.settings;
Hope this helps!

Reading files with Node.js from input file type selector

I am trying to select files using input file type and then upload them to dropbox using the dropbox Core API or saving it to a local folder using Node.JS readFile and writeFile methods. The problem is that most of these methods require the file path and all I have is the name of the file that is stored in the File object array and for what I have read browsers do not allow to get the full path for security reasons. I don't know how to go about this, can anyone help me solve this? Thanks!!