0

I am receiving an array of nested JSON, I have to read the JSON and write its data in a new file.

   "users": [
   {
      "user_id": "B781C132CAAC45CAB557F29FEA20C4BA",
      "$distinct_id": "C621631D24A6428589C8C94BAA18EA3B",
      "$properties": {
         "Platforms": "["web"]",
         "trial_class_flow": true,
         "Subscription Plan": null,
         "Autorenew Credit Card": "false",
         "Username": "midi31",
         "$email": "auto_test_368107_gaylene.turcotte@splashmath.com",
         "$first_name": "Player",
         "$last_name": null,
         "User Type": "student",
         "Paid": false,
         "Paid Once": false,
         "Created At": "2019-08-07T12:11:23Z",
         "Unsubscribe Id": "A87B13E627D24CE5BA660100E98F99D4",
         "First Platform": "web",
         "First App ShortCode": "",
         "Signup Flow": "trial_class_25",
         "School Name": "NA"
      }
   }
]

Unable to think how to model it in java.

  • 1
    You can use a library like jackson: https://github.com/FasterXML/jackson Here is a tutorial: http://www.studytrails.com/java/json/java-jackson-serialization-list/ – Jackyjjc Aug 08 '19 at 07:27
  • You can refer examples here :https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – user Aug 08 '19 at 08:04

2 Answers2

1

There is a nice online utility that does this for you. Paste your JSON to the editor and this would give you the required POJO.Once you have the POJO, you can use it to do whatever you want.

For record, there are other tools also that does the same thing. I happen to use this one more often, so giving the URL.

Check this out: http://www.jsonschema2pojo.org/

Swagata Acharyya
  • 647
  • 4
  • 10
  • There's a problem: Unexpected character ('?' (code 8220 / 0x201c)): was expecting double-quote to start field name (line 2, column 9) Getting this error. Please help me with it. – Himanshu Pandey Aug 08 '19 at 09:25
  • Change that to ". Also some other misplaced quotes. Clean them up. Should work fine. – Swagata Acharyya Aug 08 '19 at 10:39
0

You can do this using GSON library as follows :

Gson gson = new Gson();
MyModel myModel = gson.fromJson(json, MyModel.class);

OR

You can Jackson library too.

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17