Import JSON via http vs require - json

I came across two ways to import local json files to my code.
Using angulars http get.
Thats well known for loading json input. You can switch easily from remote to local json files.
Typescript require
Another way to load json in typescript files is via require. This is simple as I don't have to deal with Promises/Observables. I just include them like this:
data: any = require('assets/json/my.json');
I want to know something about the advantages and disadvantages between these two approaches. Is there a prefered way and why?

Hi it depends on your requirement.
If your file is constant, will not be changed then it is best option is to use .require()
- .require() will cache your file, and when you import again it will give the cached file, so it might be bad option you want current time data because you will not get the updated data from that file
But if your file is getting updated then you have to use HTTP.

Related

import json without caching the file (next.js or node.js)

I built an api with next.js.
I use a JSON file as a data source. I import it as a module. But if the content of the JSON changes, it still shows the first content, the same, when i started the server.
Is there a way to avoid caching JSON with import?
I need to get the JSON content, but also the updates in the JSON file, without restarting my api.
If your Server returns the JSON files with a specific File-Extension like .json you could try to turn off the caching for those file-types:
Here is an example for ngnix-servers
Here is an example for apache-servers
Another possibility is to load the JSON via Javascript where you add some random parameter to the Query-Params of the URL
Here is the Example

How can I use any json or array or any js file in .testcaferc.json?

I have created one file .testcaferc.json that contains all configuration information like browser name, specs, timeouts etc. I want to fetch the configuration data from file so that I have to change the information at only one place
I want to store all these information in single file, I tried, js, json and array. But I can not import all above format files in my .testcaferc.json, when I press Alt+F8 I see the error "Expected a JSON object, array or literal"
Is there any way I can import json, array or js data in .testcaferc.json?
Thanks in advance!!
The JSON format doesn't support any import directives. The TestCafe configuration file (.testcaferc.json) is a simple JSON file. So, the TestCafe configuration file doesn't support such functionality.
To achieve your goal, you can transform the existing .testcaferc.json file before test running: load data from various sources and add/replace values for the appropriate data fields.
Also, there is a suggestion in the TestCafe GitHub repository, which will make your scenario easier to implement. Track it to be notified about its progress.

Storing database json file in optimal way for react native app

I’m using react native, redux and firebase and I have a 1 MB database export that contains food nutritional information from the Canadian Nutrition File database.
This data will never change and is used in a FlatList users can search and import from.
What’s the optimal way for me to store this for my users to access as fast as possible when searching?
I am thinking of simply including the json file with my other source files and calling it from there using require.
But since I have firebase and redux all setup I wondered if it made more sense for whatever reason to consider those options instead.
Thanks in advance for any input.
Since you are already using redux, i'd go for adding the json content to the store on app init/mount so you can get it easily from everywhere.
Another options i'd consider:
Import, parse & render the JSON file stactically on runtime (as it never changes);
Use the AsyncStorage to make it available in local database but outsite the redux store thing.

Open local JSON file for examination

I was wondering if it's possible to open a local JSON file so I can just check its structure? Didn't/don't want to upload the file to an online JSON format checker site and was hoping I can just utilize PAW to do that.
Don't seem to be able to do this with a local file, unless I run it through a local server, eg using MAMP, unless I missed something...?
Thanks.
You could copy the content into the txt body then switch to the JSON body this will let you view it in the nice structure, sorry currently no way to directly import a file need to copy past the content.
Take a look at jsonlint npm module. Supports JSON schema validation and pretty printing.

Indexing flat XML files in elasticsearch

I'm working on a specific project where external data provided by external providers is to be indexed on our ElasticSearch Engine.
The data is provided as XML flat files.
The idea here is to script something out that reads each file, parse it and launch as many HTTP POST as needed for each one of them.
Is there a simpler way to do this? something like uploading the XML file that gets indexed automatically without any script?
You can use logstash with an xml filter to do this. Takes a bit of work to get setup the first time, but it's the most straightforward way to do it.