JSON Output of Solr NamedList


There are different JSON output format for Solr NamedList, as said in JSON Response Writer, and org.apache.solr.response.JSONWriter.

We can controls the output format of NamedLists by parameter json.nl, its value van be flat, map, arrarr, arrmap.

We can check its output by running test case: org.apache.solr.request.JSONWriterTest.testJSON(): use different value for json.nl.
Basically when json.nl=flat, the response would be like: [name1,val1, name2,val2], when json.nl=map, the response would be like: [name1:val1, name2:val2], when json.nl=arr, the response would be like:  [[name1,val1], [name2, val2]], when json.nl=arrmap, the response would be like: [{name1:val1}, {name2:val2}].
If not specified, it would use flat mode.

Usually we should use json.nl=map, so client code can easily parse the response and convert it to javascript object.

We can specify default json.nl value in solrconfig.xml when we declare our request handler: in default section or in invariants section if we don't want user to change it.

Another option is in our code, we can use subclass of NamedList: SimpleOrderedMap. It will use map mode as the JSON output.
org.apache.solr.response.JSONWriter.writeNamedList(String, NamedList)
  public void writeNamedList(String name, NamedList val) throws IOException {
    if (val instanceof SimpleOrderedMap) {
      writeNamedListAsMapWithDups(name,val);
    } else if (namedListStyle==JSON_NL_FLAT) {
      writeNamedListAsFlat(name,val);
    } else if (namedListStyle==JSON_NL_MAP){
      writeNamedListAsMapWithDups(name,val);
    } else if (namedListStyle==JSON_NL_ARROFARR) {
      writeNamedListAsArrArr(name,val);
    } else if (namedListStyle==JSON_NL_ARROFMAP) {
      writeNamedListAsArrMap(name,val);
    }
  }



Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)