Read & Write JSON with esProc

Question

I have a JSON file as shown below:

 

{

 

 "status":0,

 

 "message":"ok",

 

 "results":\[

 

 {

 

 "name":"重庆秦妈火锅(马仁山东路店)",

 

 "location":{

 

 "lat":31.312634,

 

 "lng":118.406372

 

 },

 

 "address":"弋江区马仁山东路275号(南瑞世纪联华超市向东200米)",

 

 "telephone":"0553-5919177",

 

 "uid":"d01b6bf9e9ea6f9e4d776e95",

 

 "detail_info":{

 

 "type":"cater",

 

 "tag":"火锅,餐饮",

 

 "detail\_url":"http://api.map.baidu.com/place/detail?uid=d01b6bf9e9ea6f9e4d776e95&output=html&source=placeapi\_v2",

 

 "price":"45",

 

 "overall_rating":4.5,

 

 "service_rating":2,

 

 "environment_rating":4.5,

 

 "image_num":"30",

 

 "groupon_num":4,

 

 "comment_num":"283"

 

 }

 

 }

 

 \]

 

}

 

I need to retrieve the results field and write it to a new JSON file.

 

Answer

It’s inconvenient to handle JSON files in Java. The Structured Process Language (SPL) offers encapsulated functions to do this in a simple and easy way:

A

1

=json(file(“D:/test/json   example.json”).read())

2

=json(A1.results)

3

=file(“D:/test/json   results.json”).write(A2)

A1: Read in the JSON file content as a string and store the string as a table sequence.

undefined

A2: Export A1’s results field as a JSON string.

undefined 

A3: Output A2’s JSON string to a target JSON file.

undefined