URN references to JSON converter

153 Views Asked by At

I want to convert URL References to JSON and vice versa

(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 js tools available?

Its hard to build and decode the output.

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

1

There are 1 best solutions below

0
On

Here is a surely optimizable regex replace chain who does the trick.

Given a targeting object adhering to this type:

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 takes it as input and converts it to the 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, "");

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":[]}}]}}

outputs to

(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%3AadTargetingFacet%3AyearsOfExperienceRanges:List())))))