Generate a JSON object from a given JSON template

Hello, I am currently working on creating a JSON instance from a given JSON schema.

I would greatly appreciate a Typescript solution, but I am open to any other suggestions as well.

Below is a sample of the schema file: sample-schema.json. My goal is to develop a function that can convert such schema into a schema instance.

For example, in the sample-schema.json file:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "address": {
      "type": "object",
      "properties": {
        "street_1": {
          "type": "string"
        },
        "street_2": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "state": {
          "type": "string",
          "enum": [
            "AL",
            "AK",
            "AS"
          ]
        },
        "zip_code": {
        "type": "string"
        }
      }
    },
    "birthday": {
      "type": "string"
    },
    "notes": {
      "type": "string"
    }
  },
  "required": [
    "last_name"
  ]
}

The desired output should be as follows:

{
"first_name": "",
"last_name": "",
"last_name": "",
"address": {
"street_1": "",
"street_2": "",
"city": "",
"state": "",
"zip_code": ""
},
"birthday": "",
"notes": ""
}

When calling the function with the provided schema, it should generate the corresponding JSON instance. Despite my efforts to find a solution through Google, all results pertained to converting JSON to JSON schema.

Answer №1

After doing some research, I stumbled upon the JavaScript code on this json-schema-filter repository.

Answer №2

retrieve the content of the sampleSchema variable and apply the JavaScript JSON.parse(sampleSchema) function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript framework that is easily customizable to include support for XmlHttpRequest.onprogress, even if it needs to be emulated

Which JavaScript library or framework offers support for the "onprogress" event for XmlHttpRequest, even if it needs to be emulated using a plugin or extension? Alternatively, which JavaScript framework is the most straightforward to extend in order to add ...

Decoding a changeable JSON structure

As I am working on parsing a JSON and looking for a specific key within the JSON object, I am facing an issue due to the changing structure of the JSON. It is not feasible to hard code the path for searching. Are there any alternative methods to parse this ...

Establishing Routing for Angular within an ASP.NET Web API 2 application

Currently, I am facing difficulties in setting up the routing for my project. There are several cases that need to be handled, but I am struggling to make them work as intended. case 1: / - Should route to the index of the angular app Case 2: /{angular ...

Tips for altering the class of an HTML button dynamically when it's clicked during runtime

I currently have a set of buttons in my HTML page: <button id="button1" onclick="myFunction()" class="buttonClassA"></button> <button id="button2" onclick="myFunction()" class="buttonClassA"></button> <button id="button3" onclic ...

Calculator: Show individual digits once more following a calculation

After clicking the 'equal' button, the result is displayed. However, if I click another number, it doesn't clear the result. I want the result to stay when operation symbols like '+' or '/' are pressed. While working on t ...

Is it possible for a component to have multiple templates that can be switched based on a parameter?

Scenario My task is to develop a component that fetches content from a database and displays it on the page. There needs to be two components working together to create a single page, following a specific component tree structure. DataList - receives ful ...

jquery technique for toggling a single button

My goal is to use jQuery to toggle a button rather than the typical paragraph toggling. I want the button to appear and disappear when clicked, while also increasing the "score" upon each click and decreasing it when the button reappears. Can anyone guide ...

Secure your NetSuite API calls with OAuth 1.0 signature authorization

After extensively reviewing the documentation and various posts on Stack Overflow about creating a signature for NetSuite OAuth1.0 with TBA, I believe that I have followed all of the necessary steps and correctly generated the key. However, upon making the ...

Utilizing jQuery to remove a class with an Ajax request

My setup includes two cards, one for entering a postcode and another with radio buttons to select student status (initially hidden). An Ajax request validates the postcode input - turning the card green if valid (card--success) and revealing the student se ...

Are there any JavaScript charting tools that can draw in a clockwise direction with multiple data points?

Can anyone recommend a JavaScript live chart/graph library that can create clockwise graphs with multiple points, similar to the example below? https://i.sstatic.net/dQfK4.png ...

Steps for iterating through an array within an object

I currently have a JavaScript object with an array included: { id: 1, title: "Looping through arrays", tags: ["array", "forEach", "map"] } When trying to loop through the object, I am using the following ...

Retrieving data from an external API using Express.js and displaying it on a website

I'm facing a challenge in handling a solution with node.js and express.js utilizing pug for rendering HTML. I need to render on a single page by retrieving values from separate HTTP GET requests from different websites. My goal is for express/node to ...

Map Loader for GeoJson Leaflet Integration

Although my English skills are not perfect, I will do my best to explain my issue. I have some knowledge of javascript / html / ajax and I have created a webgis using Leaflet. The problem arises when loading a large geojson file onto the map - it takes qui ...

Deactivate DropDownList within Update Panel with JQuery

Below is the Update Panel that I am working on: <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="d ...

What steps can be taken to fix the recurring node error "EADDRINUSE"?

Having trouble running an http server through node as I keep encountering the EADDRINUSE error specifically on port 5000. I've attempted using sudo lsof -i tcp:5000 and sudo kill -9 [PID]. Here's a snippet of the shell command: Borealis:BackEnd g ...

What is the best way to extract individual objects from several arrays and consolidate them into a single array?

Currently, I have a collection of objects stored in a variable called listOfObjects. They are not separated by commas because I utilized the Object.entries method to extract these values from another array. console.log(listOfObjects) outputs { q: 'L ...

Next.js does not recognize the _app file

After including the _app.tsx file in my project to enclose it within a next-auth SessionProvider, I noticed that my project is not recognizing the _app.tsx file. Even after adding a div with an orange background in the _app.tsx file, it does not reflect in ...

How to extract data from Facebook's JSON using Javascript

I am utilizing a JavaScript API to extract Facebook comments. After receiving the JSON result below, I'm wondering how I can unpack and utilize them on my webpage? { "id": "1234567891_2823098717038_3160191", "from": { "name": "U ...

Is there a way to calculate the total of form field values in Vue.js?

I am currently working on a project that involves calculating the total sum of 5 input fields. I have set up data fields and v-model for each field, and in my computed section I am attempting to calculate the sum. <input type="text" class="form-contro ...

Tips for utilizing withNavigation from react-navigation in a TypeScript environment

Currently, I am working on building an app using react-native, react-navigation, and typescript. The app consists of only two screens - HomeScreen and ConfigScreen, along with one component named GoToConfigButton. Here is the code for both screens: HomeSc ...