JSON to URN Converter: A tool for converting JSON references

Is there a way to convert URL references to JSON and back again programmatically?

(include:(and:List((or:(urn:li:adTargetingFacet:locations:List(urn:li:geo:102221843))
),(or:(urn:li:adTargetingFacet:skills:List(urn:li:skill:17))))))

Are there any JavaScript tools that can help with this process?

The conversion seems challenging without the right tools.

References: https://learn.microsoft.com/en-us/linkedin/shared/references/v2/ads/targeting-criteria?context=linkedin%2Fmarketing%2Fcontext

Answer №1

Here's a highly efficient regex replace chain that gets the job done.

If you have a targeting object structured like this:

type LinkedinTargetingCriteria = {
  include: {
    and: [
      {
        or: {
          "urn:li:adTargetingFacet:degrees": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:employers": string[];
          "urn:li:adTargetingFacet:staffCountRanges": string[];
          "urn:li:adTargetingFacet:industries": string[];
          "urn:li:adTargetingFacet:growthRate": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:titles": string[];
          "urn:li:adTargetingFacet:seniorities": string[];
          "urn:li:adTargetingFacet:jobFunctions": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:fieldsOfStudy": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:locations": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:schools": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:skills": string[];
        };
      },
      {
        or: {
          "urn:li:adTargetingFacet:yearsOfExperienceRanges": string[];
        };
      }
    ];
  };
};

This function will take that input and convert it into a formatted targeting string:

export const linkedinTargetingToString = (
  targeting: LinkedinTargetingCriteria
): string =>
  JSON.stringify(targeting)
    .replace(/:/g, "%3A")
	.replace(/%3A{/g, ":(")
	.replace(/{/g, "(")
	.replace(/}/g, ")")
	.replace(/%3A\[/g, ":List(")
	.replace(/\[/g, "List(")
	.replace(/]/g, ")")
	.replace(/"/g, "");

For instance, when given this json:

{"include":{"and":[{"or":{"urn:li:adTargetingFacet:degrees":[]}},{"or":{"urn:li:adTargetingFacet:employers":[],"urn:li:adTargetingFacet:staffCountRanges":[],"urn:li:adTargetingFacet:industries":[],"urn:li:adTargetingFacet:growthRate":[]}},{"or":{"urn:li:adTargetingFacet:titles":[],"urn:li:adTargetingFacet:seniorities":[],"urn:li:adTargetingFacet:jobFunctions":[]}},{"or":{"urn:li:adTargetingFacet:fieldsOfStudy":[]}},{"or":{"urn:li:adTargetingFacet:locations":["urn:li:geo:103350119"]}},{"or":{"urn:li:adTargetingFacet:schools":[]}},{"or":{"urn:li:adTargetingFacet:skills":[]}},{"or":{"urn:li:adTargetingFacet:yearsOfExperienceRanges":[]}}]}}

The output will be:

(include:(and:List((or:(urn%3Ali%3AadTargetingFacet%3Adegrees:List())),(or:(urn%3Ali%3AadTargetingFacet%3Aemployers:List(),urn%3Ali%3AadTargetingFacet%3AstaffCountRanges:List(),urn%3Ali%3AadTargetingFacet%3Aindustries:List(),urn%3Ali%3AadTargetingFacet%3AgrowthRate:List())),(or:(urn%3Ali%3AadTargetingFacet%3Atitles:List(),urn%3Ali%3AadTargetingFacet%3Aseniorities:List(),urn%3Ali%3AadTargetingFacet%3AjobFunctions:List())),(or:(urn%3Ali%3AadTargetingFacet%3AfieldsOfStudy:List())),(or:(urn%3Ali%3AadTargetingFacet%3Alocations:List(urn%3Ali%3Ageo%3A103350119))),(or:(urn%3Ali%3AadTargetingFacet%3Aschools:List())),(or:(urn%3Ali%3AadTargetingFacet%3Askills:List())),(or:(urn%3Ali%3AadTar...

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

Tracking your daily nutrition using cronometer with the powerful combination of JavaScript

I'm currently developing a JavaScript cronometer in vueJS. I want the timer to start at 5 minutes (300 seconds) and then continue counting minutes and seconds. Although my cronometer is functioning, I am struggling to get it to start counting minutes ...

How can I utilize JavaScript to seamlessly loop through and display these three images in succession?

My current code is HTML, CSS, and JS-based. I am looking to design three of these div elements that appear and hide in a loop every 3-5 seconds. After the first run, I want the execution to repeat continuously. How can I modify my code to achieve this func ...

Google App Engine does not properly interpret PHP code when making AJAX requests

I am currently facing an issue with using AJAX request on Google App Engine. In my local development environment, everything works fine and the request is correctly interpreted. However, when I deploy the code to production, the AJAX request renders the co ...

Issue with the functionality of socket.io callback functions

Previously, I used to utilize the socket.io emit callback in the following manner: On the client side: mysocket.emit('helloworld', 'helloworld', function(param){ console.log(param); }); On the server side: var server = r ...

Efficiently update numerous users simultaneously within Firebase

I am trying to figure out how to update multiple user objects stored in my Firebase 2.x DB simultaneously. Each object is structured similarly to the following example: { "users": { "$id": { "date_of_birth": "June 23, 1912", "full_name": ...

Challenges in extracting data from JSON using asmx

I'm facing an issue with parsing a JSON string received from my ASMX webservice through IIS. The string being received looks like this: "{\"Name\":\"Waqas Aslam\",\"Company\":\"ABC Systems AB\",\"Address&b ...

The vanishing AJAX button

Currently, I am working on a web app in Laravel that includes a simple textarea within a form. This form allows users to input basic markdown text, and my goal is to display the data with both HTML tags and normal formatting. However, I encountered an issu ...

Any suggestions on how to address vulnerabilities in npm when upgrading the main dependency version is not an option?

I recently ran the npm audit --production command and found a high-risk vulnerability related to the snowflake-sdk dependency. After checking the snowflake github page, I noticed that the package.json file includes "requestretry": "^6.0.0&qu ...

What is the method to obtain an object as the return value from a click function in JavaScript?

I would like to retrieve the cell value from a table by clicking on a button. I have already created a function called getDetail in JavaScript that is implemented on the button, but I am facing difficulty in returning the value from the function. <butto ...

transferring data from ejs template

In my app.js file, I have an array that stores files. To display these files on a website, I use a for-loop in ejs: <% for(let i = 0;i<posts.length; i++){ %> <li class="listtitle"> <%= posts[i].title %> </li> ...

Connect an Angular Service object to a Controller's Scope and keeping it in sync

I am facing an issue with the interaction between my EmployeeService and EmployeeController. The service contains a specific object which I bind to the controller's scope: app.controller('EmployeeController', function($scope, EmployeeServic ...

Accessing the element within an ion-tab using document.getElementById

Within my ion-view, I have ion-tabs containing a canvas element. However, when attempting to retrieve the canvas using document.getElementById('photoCanvas'); I receive 'undefined'. Here is the code snippet: HTML: <ion-view ...

Eliminating JSON unicode literals in Delphi/dwsJSON: A step-by-step guide

When I view a JSON string in UTF-8 format on Google Chrome, it appears like this (without new lines): {"_links": {"self": {"href": "http://bla:8888/1/2/3/2257487e4a750cab"}, "it\u0119m": [{"href": "http://bla:8888/1/2/4/8f4fea003fe4c7fb284801d082de3 ...

Looking for a JavaScript code to create a text link that says "submit"?

Here is the code I have, where I want to utilize a hyperlink to submit my form: <form name="form_signup" id="form_signup" method="post" action="/" enctype="multipart/form-data"> ... <input type="submit" value="Go to Step 2" name="completed" /> ...

What is the best way to organize class usage within other classes to prevent circular dependencies?

The engine class presented below utilizes two renderer classes that extend a base renderer class: import {RendererOne} from "./renderer-one"; import {RendererTwo} from "./renderer-two"; export class Engine { coordinates: number; randomProperty: ...

What's the most effective method: Utilizing generic code generation or hardcoding API calls?

Currently, I find myself in a situation where I am contemplating the best practice for handling my work on an Ecommerce website that involves orders, shipments, invoices, and more. In addition to creating the UI for my application, I also have the capabil ...

Using jQuery to send a POST request with a data object

Trying to figure out something. Attempting to post an object using jQuery Ajax POST, like so: var dataPostYear = { viewType:GetViewType(), viewDate:'2009/09/08', languageId:GetLanguageId() }; $.ajax({ type: "POST", url: url ...

Discovering the correct location within a complex JSON or array to perform updates using JavaScript (either AngularJS or vanilla JavaScript

I am currently facing a challenge where I need to search for a specific value within my complex JSON array and then update the corresponding object. Here is a snippet of my JSON array: var myArray = [{ "id": 5424, "description": "x ...

Transforming Object into JSON Array using JSON

I am working on converting a class object generated via Reflection into a JSON string. Below are my methods: public Object creatObjectAsString(String className) { Object objects = null; try { objects = java.lang.reflect.Array.newInstance( ...