1

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,
          }
        ]
      }
    }
  }
}
6
  • 3
    Can you please show a sample of the JSON? Commented Jun 6, 2014 at 10:31
  • Update the post with the link to jsfiddle. Commented Jun 6, 2014 at 10:39
  • I couldn't find any nesting in your sample that might require recursion to achieve this (might have not looked too well) Can you please post a sample of JSON where you think recursion is required? Commented Jun 6, 2014 at 10:42
  • Updated the jsfiddle link with the required json format. The required json format is pasted in the html section and the current format is pasted in the jacascript section Commented Jun 6, 2014 at 10:48
  • Could you add the JSON before and after examples to the question. Not just link to JSFiddle. You never know, JSFiddle could disappear tomorrow. Commented Jun 6, 2014 at 11:04

1 Answer 1

1

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))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.