zilcuanu
zilcuanu

Reputation: 3715

transforming json data using recursion

I have a json data which I need to transform using javascript. The json result is obtained from the server when a user search with keyword. Some objects will be empty and some will contain the data in the search result json. I need to transform into another json which should contain only the objects which has totalRecords attribute value more than 0.

I am adding the link to jsfiddle for the json to be transformed. http://jsfiddle.net/Xhhn4/3/

I tried using recursion to solve the problem. The code for which is below:

  var resultData=data.results;
      var contents = [];
      var get_content=function(resultdata){
        console.log(resultdata);
        $.each(resultdata, function( index, value ) {
          value=value || {};
          if (value.hasOwnProperty("content") && value.content.length > 0 ) {
            contents.push({name:index,value:value});
          }else{
              get_content(value);

          }
        });
      }

      get_content(resultData);
      console.log("Printing the content");
      console.log(contents);

But the above code will give me an array of objects which has content and does not give me the entire tree structure from its root. I Basically need the entire json in the same format with just the empty object removed. Can we achieve this using recursion and how to consolidate the results after the divide with divide and conquer method?

Json Before:

{
  "keywords": [
    "keyword1"
  ],
  "results": {
    "country": {
      "general_search_results": {
        "hospitalDetails": {
          "totalRecords": 0,
          "content": []
        },
        "schoolsDetails": {
          "totalRecords": 0,
          "content": []
        },
        "shoppingMartDetails": {
          "totalRecords": 0,
          "content": []
        }
      },
      "companies_search_results": {
        "totalRecords": 5,
        "content": [
          {
            "companyName": "AAA",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "BBB",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "CCC",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "DDD",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "EEE",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          }
        ]
      },
      "vehicles_search_results": {
        "twoWheelers": {
          "totalRecords": 0,
          "content": [],
        }
      },
      "accidents_search_results": {
        "totalRecords": 0,
        "content": [],
      }
    },
    "state1": {
      "general_search_results": {
        "hospitalDetails": {
          "totalRecords": 0,
          "content": []
        },
        "schoolsDetails": {
          "totalRecords": 0,
          "content": []
        },
        "shoppingMartDetails": {
          "totalRecords": 0,
          "content": []
        }
      },
      "companies_search_results": {
        "totalRecords": 5,
        "content": [
          {
            "companyName": "AAA",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "BBB",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "CCC",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "DDD",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "EEE",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          }
        ]
      },
      "vehicles_search_results": {
        "twoWheelers": {
          "totalRecords": 0,
          "content": [],
        }
      },
      "accidents_search_results": {
        "totalRecords": 0,
        "content": [],
      }
    }
  }
}

Json After format:

{
  "keywords": [
    "keyword1"
  ],
  "results": {
    "country": {
      "companies_search_results": {
        "totalRecords": 5,
        "content": [
          {
            "companyName": "AAA",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "BBB",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "CCC",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "DDD",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "EEE",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          }
        ]
      }
    },
    "state1": {
        "companies_search_results": {
        "totalRecords": 5,
        "content": [
          {
            "companyName": "AAA",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "BBB",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "CCC",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "DDD",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "EEE",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          }
        ]
      }
    }
  }
}

Upvotes: 1

Views: 90

Answers (1)

Chris Vouga
Chris Vouga

Reputation: 554

This should do the trick

I called the function "prune" because the definition of prune means to trim a tree so it seemed fitting.

const isObject = (o) =>
  typeof o == 'object' && o.constructor == Object;

const isEmptyObject = (o) =>
  Object.entries(o).length === 0

const hasProperty = (key, o) =>
  o.hasOwnProperty(key)

const isPositive = (x) =>
  x > 0

const isEmptyArray = (arr) =>
  arr.length === 0

const pruneReducer = (obj, [key, value]) => {
  if(isObject(value)) {
    if(hasProperty("content", value)) {
      if(isEmptyArray(value.content)) {
        return obj
      } else {
        obj[key] = value
        return obj
      }
    } else {
      const childObj = prune(value)
      if(isEmptyObject(childObj)) {
        return obj
      } else {
        obj[key] = childObj
        return obj
      }
    }
  } else {
    obj[key] = value
    return obj
  }
}

const prune = (obj) =>
  Object.entries(obj).reduce(pruneReducer, {})


const data = {
  "keywords": [
    "keyword1"
  ],
  "results": {
    "country": {
      "general_search_results": {
        "hospitalDetails": {
          "totalRecords": 0,
          "content": []
        },
        "schoolsDetails": {
          "totalRecords": 0,
          "content": []
        },
        "shoppingMartDetails": {
          "totalRecords": 0,
          "content": []
        }
      },
      "companies_search_results": {
        "totalRecords": 5,
        "content": [
          {
            "companyName": "AAA",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "BBB",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "CCC",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "DDD",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "EEE",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          }
        ]
      },
      "vehicles_search_results": {
        "twoWheelers": {
          "totalRecords": 0,
          "content": [],
        }
      },
      "accidents_search_results": {
        "totalRecords": 0,
        "content": [],
      }
    },
    "state1": {
      "general_search_results": {
        "hospitalDetails": {
          "totalRecords": 0,
          "content": []
        },
        "schoolsDetails": {
          "totalRecords": 0,
          "content": []
        },
        "shoppingMartDetails": {
          "totalRecords": 0,
          "content": []
        }
      },
      "companies_search_results": {
        "totalRecords": 5,
        "content": [
          {
            "companyName": "AAA",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "BBB",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "CCC",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "DDD",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          },
          {
            "companyName": "EEE",
            "location": "bangalore",
            "employees": "2000",
            "reputation": 4,
          }
        ]
      },
      "vehicles_search_results": {
        "twoWheelers": {
          "totalRecords": 0,
          "content": [],
        }
      },
      "accidents_search_results": {
        "totalRecords": 0,
        "content": [],
      }
    }
  }
}

console.log(prune(data))

Upvotes: 1

Related Questions