Utilizing a Jackson-inspired library for handling JSON property names containing dots in Javascript

Is there a way to create a Json with field names that contain dots '.' in a JavaScript application (specifically, TypeScript)?

This is the expected Json output:

{
    "tasks.max": "1",
    "key.converter": "io.confluent.connect.avro.AvroConverter",
    "topics": "eraseme_topic_schema_test_v05"
 }

I have tried using libraries like ts-jackson and jackson-js. Below is an example code snippet using ts-jackson:

MyDomain.ts

@Serializable()
import { JsonProperty,Serializable } from 'ts-jackson';
export class MyDomain{
        @JsonProperty({path: 'tasks.max', elementType: String})
        tasks_max: string;
        @JsonProperty({path: 'key.converter'})
        key_converter: string;
        @JsonProperty({path: 'topics'})
        topics: string;
}

However, the properties containing dots are being created as subclasses in the Json output instead of direct property fields with dots.

{
  tasks: { max: '1' },
  key: { converter: { schema: [Object] } },
  topics: 'eraseme_topic_schema_test_v05'
}

Is there a way to achieve this functionality in JavaScript by using a Jackson-like library?

Answer №1

In a comment on the lodash repository, joniba provided an example of how to access properties with dots using lodash:

// lodash set
const o = {}
_.set(o, 'a["b.c"]', 1)
// returns { a: { 'b.c': 1 } }

This method also works with lodash get:

// lodash get
const obj = {'a.b': 'value'};
_.get(obj, '["a.b"]'); // 'value'

Applying this concept, we can define the ts-jackson path properties like this:

export class MyDomain{
    @JsonProperty({path: '["tasks.max"]', elementType: String})
    tasks_max: string;
    @JsonProperty({path: '["key.converter"]'})
    key_converter: string;
    @JsonProperty({path: 'topics'})
    topics: string;
}

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

"Step-by-step guide on populating a select box with data from the scope

Hey everyone, I'm new to using Angular and hoping for some help with a simple question. I've created a form (simplified version below) that I want users to see a live preview as they fill it out. Everything was going smoothly with regular field ...

Easily Organize Your Data with Kendo React Grid: Rearrange Columns and Preserve Your Custom Layout

Currently, I am working on implementing a Kendo React grid with the option set to reorderable={true} for allowing column reordering. However, I am facing difficulty in determining how to save the new order of columns. Which event should I use to detect whe ...

"Utilize Google Sheets to extract and monitor Best Buy prices through web scraping

Looking to automate the process of scraping product prices into Google Sheets. Previously used =importrange(A1,B1) A1 = B1 = /html/body/div[3]/main/div[2]/div/div[1]/div[3]/div[2]/div/div[1]/div[1]/div/div/div/div/div/div/div[2]/div[1]/div[1]/div/span[1] ...

Converting JSON to CSV using JQ command in Linux shell

Seeking to convert a JSON into a CSV file where each Line Item Object is on a separate line, and that line includes the PURCHASEORDERNO. Attempting to retrieve the PURCHASEORDERNO of the HEADER array from the LINE ARRAY using .HEADER[].PURCHASEORDERNO. En ...

Generate a Vue project using the vue-cli-service build command, but be sure to include the necessary period in

After completing my Vue project, I encountered an issue when running the script 'npm run build' (vue-cli-service build). The output files were generated as expected, but the paths to all js and css files in the index.html file were incorrect. For ...

The imgAreaSelect plugin is up and running successfully. Now, I am looking to utilize the x and y coordinates to make updates to the image stored in the database. How can I

After retrieving the dimensions and coordinates from the jQuery plugin imgAreaSelect, I am looking for guidance on how to update the image in my database based on this selection. The database contains a tempImage and Image field, and my goal is to allow ...

Dynamically add index to attribute as it updates

Having an issue with my dynamic button element: <button v-on:click="changeRecord(element)" v-b-modal.modal-5>Aendern</button> This button is generated dynamically within a v-for loop. Instead of manually including the attribute name like v-b- ...

Pnotify encounters a glitch where the buttons are not displayed when utilized with jquery

Using pnotify with jQuery and Bootstrap3, I am facing an issue where the buttons do not appear. In order to address this problem, I have ensured that both pnotify.custom.min.css and pnotify.custom.min.js files are included in the header of the application ...

Extract each line of text from the ul li tags at every level of the hierarchy with the help of

I am looking to extract a list of strings from the ul and li elements with slash (/) separating each level up to the nth level using JavaScript. I attempted to use both the Jquery.map and each functions, but unfortunately, I was unsuccessful. ...

Automated login feature in JQuery utilizing localStorage

I've been working on implementing an automatic login feature for users using the "Remember Me" functionality. Below is the code I have written, but unfortunately, it's not logging in users automatically: if (localStorage.getItem("username") != ...

How to maintain global position, rotation, and scale when adding an object to a group in Three.js

I need to transfer an object from one group (or world/scene) to another group while maintaining its global transformation. I want the object to appear unchanged. Here is an example of what I am trying to achieve: //store the current world transformation ...

Working with a variety of JSON object types within a single JSON array with Retrofit2

When the server responds, I get a variety of JSON objects based on their "type". Each response type has its own bean. Can Retrofit automatically use the correct Bean class for each JSON object in the list? { "cards": [2] 0: { " ...

Unable to load Highcharts from php json, however it is able to load from data.json file

I attempted to create a highchart using JSON data. I created a PHP file that generates the JSON and inserted the address into the getJSON() function like this: $(document).ready(function() { $.getJSON("data.php", function(json) { chart = new ...

The Wordpress plugin's Ajax function is giving back a response value of zero

I'm facing an issue where I am attempting to send AJAX data to my wordpress table, but the response I receive from my PHP script is always a 0. This problem arises specifically on the admin side of things. Can anyone provide assistance? Furthermore, ...

What is the best way to import the axios post method from a JavaScript file into a Vue component

I'm encountering an issue when trying to export the data received from the API in my JavaScript file and display it in my Vue file. Currently, I am sending a security JSON to the server using the POST method. Although I am able to retrieve the output ...

The dual-file upload feature of the jQuery ajaxForm plugin

After extensive searching online, I have yet to find an answer to my problem. The issue at hand is that when using the ajaxForm malsup plugin, it submits my files twice. HTML: <form id="gallery" enctype="multipart/form-data" action="/upload-gallery.p ...

Swapping out components or features within AngularJS

Is it possible to make my dependencies interchangeable in AngularJS? For example, if I have a service named myService stored within the module myDependency, how can I switch out myDependency with a new service without disrupting the main application? Shou ...

Tips for receiving live updates of active users online with the help of Mongoose and socket.io

I have been working on developing an app that covers various topics. Each topic has online users, and I am using express/socket.io/mongoose for the backend and flutter for the frontend. Currently, I am facing a challenge in displaying real-time informatio ...

Trouble with loading scripts after transitioning to a new page with AJAX (Barba.js)

Exploring the potential of using AJAX for smooth page transitions. Tried two different methods so far: manually coded transition from () and Barba.js approach (). With both methods, scripts work on initial load but fail to execute when navigating to anot ...

Resolving a CSS Layout Dilemma: How to Overlay Sidebar on Wrappers

I've run into a challenge while attempting to position a sidebar over page wrappers. The issue with absolute positioning arises when the sidebar needs to align with the corner of the blue header. I have contemplated using JavaScript to maintain its cu ...