Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json to typescript #1458

Open
akshykhade opened this issue Nov 18, 2024 · 1 comment
Open

Json to typescript #1458

akshykhade opened this issue Nov 18, 2024 · 1 comment
Labels
feature Feature / Tool request or idea untriaged

Comments

@akshykhade
Copy link

What feature or new tool do you think should be added to DevToys?

Json to typescript or possibaly multiple language classes or interfaces conversion

Why do you think this is needed?

its everyday job. i use - https://app.quicktype.io/

Solution/Idea

https://app.quicktype.io/

Comments

No response

@akshykhade akshykhade added feature Feature / Tool request or idea untriaged labels Nov 18, 2024
@yuvashrikarunakaran
Copy link

bash
npm install quicktype-core

js
const { quicktype, InputData, jsonInputForTargetLanguage } = require("quicktype-core");

/**

  • Convert JSON to TypeScript interface.

  • @param {string} jsonData - The JSON data string to convert.

  • @returns {string} - The generated TypeScript code.
    */
    async function convertJsonToTypescript(jsonData) {
    try {
    const inputData = new InputData();
    const jsonInput = jsonInputForTargetLanguage("typescript");

    // Add the JSON data to the InputData
    inputData.addSource({
    name: "MyModel",
    samples: [jsonData]
    });

    // Generate TypeScript from JSON
    const { lines } = await quicktype(inputData, "MyModel", { lang: "typescript" });
    return lines.join("\n");
    } catch (error) {
    console.error("Error converting JSON:", error);
    }
    }

// Example JSON
const jsonData = { "name": "John Doe", "age": 30, "isActive": true };

convertJsonToTypescript(jsonData).then((typescriptCode) => {
console.log(typescriptCode);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature / Tool request or idea untriaged
Projects
None yet
Development

No branches or pull requests

2 participants