FastPrepProject JSON Fields into a Target Format
Problem · Hash Table

Project JSON Fields into a Target Format

Learn this problem
EasyBooking.com logoBooking.comFULLTIMEPHONE SCREEN

Problem statement

A JSON object has been flattened into sourceFields. Each row is [path, value], where path is a dot-separated object path and value is a valid JSON encoding of the complete value at that path.

The requested target format is given by targetFields. Each row is [outputKey, sourcePath]. Build the target object and return its fields as rows [outputKey, value] in the same order as targetFields.

  • If sourcePath exists in sourceFields, copy its encoded value unchanged.
  • If sourcePath is missing, use null, the JSON encoding of a null value.

Paths match exactly and case-sensitively. A dot is part of the path syntax; do not infer parent objects or index arrays.

Function

projectJsonFields(sourceFields: String[][], targetFields: String[][]) → String[][]

Examples

Example 1

sourceFields = [["user.name","\"Ada\""],["user.age","37"],["active","true"]]targetFields = [["displayName","user.name"],["years","user.age"],["city","user.city"]]return = [["displayName","\"Ada\""],["years","37"],["city","null"]]

The first two paths exist, so their encoded values are copied. user.city is absent, so the target field receives null.

Example 2

sourceFields = [["order.items","[1,2,3]"],["order.meta","{\"priority\":true}"]]targetFields = [["items","order.items"],["metadata","order.meta"]]return = [["items","[1,2,3]"],["metadata","{\"priority\":true}"]]

Arrays and objects remain opaque encoded values. Projection changes field names without parsing or modifying those values.

Example 3

sourceFields = [["Name","\"upper\""],["name","\"lower\""]]targetFields = [["first","name"],["second","Name"]]return = [["first","\"lower\""],["second","\"upper\""]]

Path matching is case-sensitive, and output rows preserve the requested order.

Constraints

  • 0 <= sourceFields.length <= 100000.
  • 1 <= targetFields.length <= 100000.
  • Every row in both arrays contains exactly two strings.
  • Every source path and output key is non-empty and has length at most 256.
  • Source paths are unique, and output keys are unique.
  • Every source value is a valid JSON encoding with length at most 10000.
  • The total number of characters across both inputs is at most 1000000.
drafts saved locally
public String[][] projectJsonFields(String[][] sourceFields, String[][] targetFields) {
    // Write your solution here.
}
sourceFields[["user.name","\"Ada\""],["user.age","37"],["active","true"]]
targetFields[["displayName","user.name"],["years","user.age"],["city","user.city"]]
expected[["displayName", "\"Ada\""], ["years", "37"], ["city", "null"]]
checking account