Say I have this JSON file:
{
"foo": 1,
"bar": true,
"baz": "yes"
}
is there a way to import that file and get static typing with TypeScript?
with plain Node.js, we do:
const json = require('./file.json');
but with TS, is there a way to do:
import json = require('./file.json');
is there not a way to get static typing like this? It should be easy, right?
IMO, you should just be able to do
typeof 'path/to/json/file.json'
so that would be:
export type MyInterface = typeof 'path/to/json/file.json'
and that will give you a type.
You can essentially do the same as standard Node.js but use an interface and attach it to the variable you're require-ing into.
interface File {
foo: number
bar: boolean
baz: string
}
const jsonFile: File = require('./file.json')
If the data read and parsed from the file doesn't conform to the interface, TS should throw an error.
There is no current method of dynamically analyzing an unread JSON file to acquire a type for it.
Related
I'm trying to read a JSON file in my Angular project. The file is located in the assets folder. The file has decimal values like:
{
"valueA": 0.40000000000002,
"valueB": 23.99999999999999995
}
My problem is that the values I got from importing the file are rounded to:
{
"ValueA": 0.4
"ValueB": 25
}
Is there a way to load the JSON with the exact digits from the source? Or convert them to a string? Unfortunately I have no way to change the source to split the numbers at the dot or to save them as a string. I could edit it in the pipeline that seeds the data but to me that looks like a really messy solution.
Currently I import and use the JSON like this:
import MyJson from 'src/assets/MyJson.json'
export class MyService {
private myJson = Object.assign(MyJson);
public getFieldsIWant() {
return this.myJson.theFields.iWant;
}
}
The main problem, I think, is with the first line import {.... If I print the imported File, it already "converted" the decimal place. Is there any other way to import JSON Files in TS so that this doesn't happen (already tried the import via the httpClient, same result)?
You can use libraries like https://github.com/josdejong/lossless-json to replace JSON.parse
const fs = require('fs');
const LosslessJSON = require('lossless-json');
const fileContents = fs.readFileSync('./data.json', 'utf8');
let json = LosslessJSON.parse(fileContents);
When importing a JSON file in a TypeScript project with the resolveJsonModule option enabled the TypeScript compiler is able to automatically infer the type of the imported JSON file. However, this type is too specific and I want to replace it with a more generic one. I tried to do this by writing a custom type definitions file:
// file modules.d.ts
declare module "*.json" {
const value: Record<string, string>;
export default value;
}
but the TypeScript compiler ignores this definition.
The only way that works for me is to use temporary variables, like this:
import DATA_JSON from "./data.json";
const DATA = DATA_JSON as Record<string, string>; // Use my own type instead of the inferred one.
export { DATA };
but this is boring. Is there any other way to give my own types to JSON files imported in TypeScript?
you could try it with mapped types. Not sure what structure/types you want to achieve but I believe it's the way to go.
Having such requirement sounds like a good moment to start exploring advanced TS (I recommend Matt Pocock on yt)
import DATA_JSON from './data.json';
const OWN_TYPED_DATA_JSON = DATA_JSON as unknown as {
[key in keyof typeof DATA_JSON]: {
[nestedKey in keyof typeof DATA_JSON[key]]: {
[moreNestedKey in keyof typeof DATA_JSON[key][nestedKey]]: string
}
}
}
I have a JSON file in the flutter directory, not in assets.
json_data.json:-
{
"foo" : "bar"
}
I want to read this JSON on different files.
like
myfile.dart:-
import "package:mypackage/json_data.json" as data;
import 'dart:convert';
var my_data = json.decode(data);
I am getting the error:-
The name 'data' refers to an import prefix, so it must be followed by '.'.
Try correcting the name to refer to something other than a prefix, or renaming the prefix.
What is the problem here? why can't I read JSON from local import in flutter?
You should look into loading assets in flutter. You can't simply import an arbitrary file. Importing is for source code/libraries.
You need to declare this file as an asset in your pubspec.yaml
flutter:
assets:
- json_data.json
Then in your code you can load this asset as a String:
import 'package:flutter/services.dart' show rootBundle;
Future<String> getJson() {
return rootBundle.loadString('json_data.json');
}
You can decode the JSON with your existing code, but it should be placed in a method body somewhere. You call this getJson function to retrieve the JSON String:
var my_data = json.decode(await getJson());
Alternatively, you could simplify this even further by putting the contents of your JSON file directly into the code as a String, but this may not be possible, it depends on your intended use of this JSON.
const String data = '''
{
"foo" : "bar"
}
''';
var my_data = json.decode(data);
I come from a Python Background and recently started programming using TypeScript and Angular2. I want to know how to obtain keys from a JSON object using TypeScript.
I have a response like so:
response.text()
I pass this object to a function
removeMetaData (my_data: string){
//(Various manipulation)...etc
}
i have read that I can call a json() method instead of text(). If I do that, what is the type I should use for my_data?
Then,
If my JSON looks like this:
{
"count": 100,
"next_page": "http://www.test.com/users/?page=2",
"prev_page": "http://www.test.com/users/?page=3",
"results":[
{
"username": "johnny"
},
Etc....
]
How do I parse that?
I've read I might have to use an interface but I don't understand how.
In python it's just response["next_page"] to get the key of a dictionary, then I can assign that value to a variable within the class. That is exactly what I'm trying to achieve within a component.
Thank you.
ADDITION
list() {
this.requestService.get(this.api_path)
.subscribe(
response => this.populate(response.json()),
error => this.response = error.text()
)
}
populate(payload: object) {
this.count = payload.count;
this.next = payload.next;
this.previous = payload.previous;
*payload.results => this.users = payload.results;******
}
Declare an interface which will be used as value object.
export interface IPage
{
count:number;
next_page:string;
prev_page:string;
results:Array<any>;
...
...
}
var my_data:IPage;
Now assign parsed json value to my_data and access all the properties with '.' operator i.e. my_data.count, my_data.results.
Feel free to throw any question.
If I do that, what is the type I should use for my_data?
Its just a javascript object.
As an example if you json looks like:
{
"foo": {
"bar": "bas"
}
}
Then in the parsed json (in variable someObj) the value someObj.foo.bar would be bas 🌹
I'm using JSON feeder to compare JSON output by web services as follows,
val jsonFileFeeder = jsonFile("test_data.json")
val strategy = (value: Option[String], session: Session) => value.map { jsonFileFeeder =>
val result = JSONCompare.compareJSON("expectedStr", "actualStr", JSONCompareMode.STRICT)
if (result.failed) Failure(result.getMessage)
else Success(value)
}.getOrElse(Failure("Missing body"))
val login = exec(http("Login")
.get("/login"))
.pause(1)
.feed(feeder)
.exec(http("authorization")
.post("/auth")
.headers(headers_10)
.queryParam("""email""", "${email}")
.queryParam("""password""", "${password}")
.check(status.is(200))
.check(bodyString.matchWith(strategy)))
.pause(1)
But it throws error
value matchWith is not a member of io.gatling.core.check.DefaultFindChe
ckBuilder[io.gatling.http.check.HttpCheck,io.gatling.http.response.Response,String,String]
15:10:01.963 [ERROR] i.g.a.ZincCompiler$ - .check(bodyString.matchWith(jsonFileFeeder)))
s\lib\Login.scala:18: not found: value JSONCompare
15:10:05.224 [ERROR] i.g.a.ZincCompiler$ - val result = JSONCompare.compareJSON(jsonFileFeeder, j
sonFileFeeder, JSONCompareMode.STRICT)
^
15:10:05.631 [ERROR] i.g.a.ZincCompiler$ - two errors found
Compilation failed
Here's a sample script that semantically compares a JSON response with expected output:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.json.Jackson
import java.nio.charset.StandardCharsets.UTF_8
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
lazy val expectedJson = Jackson.parse(
getClass.getResourceAsStream("/output.json"),
UTF_8
)
val scn = scenario("Scenario Name")
.exec(http("request_1")
.get("http://localhost:8000/output.json")
.check(bodyString.transform(Jackson.parse).is(expectedJson))
)
setUp(scn.inject(atOnceUsers(1)))
}
It assumes there is a file output.json in the resources directory (the directory that also contains your data and request-bodies).
However, I think you should carefully consider whether this solution is right for your needs. It won't scale as well as JSONPath or regex checks (especially for large JSON files), it's inflexible, and it seems more like a functional testing task than a performance task. I suspect that if you're trying to compare JSON files in this way, then you're probably trying to solve the wrong problem.
Note that it doesn't use jsonFile, as jsonFile is designed for use as a feeder, whereas I suspect you want to compare a single request with a hard-coded response. However, jsonFile may prove useful if you will be making a number of different requests with different parameters and expect different (known) responses. Here's an example script that takes this approach:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.json.Jackson
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
val myFeed = jsonFile("json_data.json").circular
val scn = scenario("Scenario Name")
.feed(myFeed)
.exec(http("request_1")
.get("${targetUrl}")
.check(bodyString.transform(Jackson.parse).is("${expectedResponse}"))
)
setUp(scn.inject(atOnceUsers(2)))
}
It assumes there is a json resource in data/json_data.json, that looks something like the following:
[
{
"targetUrl":"http://localhost:8000/failure.json",
"expectedResponse":
{
"success": false,
"message": "Request Failed"
}
},
{
"targetUrl":"http://localhost:8000/success.json",
"expectedResponse":
{
"success": true,
"message": "Request Succeeded"
}
}
]
The expectedResponse should be the exact JSON you expect to get back from the server. And of course you don't just have to parameterise targetUrl, you can parameterise whatever you want in this way.
As an aside, you may also be interested to know that Gatling 2.1 is expected to allow comparing an response with a file without using hacks like these (although the current development version only supports comparing byte-for-byte, not comparing-as-json).