1

I have a large file with many JSON objects similiar to the following. I need to parse everything to get the "bought_together" items as an array using the org.json library. I'm having trouble accessing anything nested in "related".

What is the required code to retrieve "bought_together" as a list?

{
  "asin": "11158732",
  "title": "Girls Ballet Tutu Zebra Hot Pink",
  "price": 3.17,
  "imUrl": "http://ecx.images-amazon.com/images/I/51fAmVkTbyL._SY300_.jpg",
  "related":
  {
    "also_bought": ["L00JHONN1S", "B002BZX8Z6"],
    "also_viewed": ["F002BZX8Z6", "B00JHONN1S", "B008F0SU0Y", "B00D23MC6W", "B00AFDOPDA"],
    "bought_together": ["D202BZX8Z6"]
  },
  "salesRank": {"Toys & Games": 211836},
  "brand": "Coxlures",
  "categories": [["Sports & Outdoors", "Other Sports", "Dance"]]
}

Here is my attempt (Please note, this is within a MapReduce program so some lines may seem out of context.):

JSONObject object = new JSONObject(sampleText); //sampleText is json that has been split by line
JSONArray boughtTogether = new JSONArray(object.getJSONArray("bought_together"));
lemon master
  • 209
  • 2
  • 5
  • 11

1 Answers1

3

using the following code, I hope it's help you.

//this will be your json object that contains and convert your string to jsonobject
//if you have json object already skip this.
JSONObject yourJSON = new JSONObject(targetString);

//getting the "related" jsonObject
JSONObject related = yourJSON.getJSONObject("related");

//getting the "bought_together" as an jsonArray and do what you want with it.
//you can act with jsonarray like an array
JSONArray bought_together = related.getJSONArray("bought_together");


//now if you run blow code

System.out.print(bought_together.getString(0));

//output is : D202BZX8Z6

-------update according to update the question------ you should change your code like this:

JSONObject object = new JSONObject(sampleText); //sampleText is json that has been split by line

JSONObject related = object.getJSONObject("related");

JSONArray boughtTogether = related.getJSONArray("bought_together");

-------update-------

i think you need to this point (it's not technicality all of they difference)

  • every thing are in {} , they will be JSONObject and the relation is key and value like :

    {"name":"ali"}

    this is a jsonobject and the value of key "name" is ali and we call it like:

    myJsonObject.getString("name");

  • every thing are in [] ,they will be JSONArray and the relation is index and value like :

    ["ali"]

    this is a JsonArray the value of index 0 is ali and we call it
    like:

    myJsonArray.getString(0);

so in your case:

  1. your total object is a JSONObject
  2. the value of "related" key is still a JSONObject
  3. the value of "bought_together" key (which is inside the value of {jsonobject} "related" key) is a JSONArray
Seyed Ali Roshan
  • 1,476
  • 1
  • 18
  • 37
  • Hmm, I don't see why this doesn't work. I'm still getting `Error: org.json.JSONException: JSONObject["related"] not found` – lemon master Nov 06 '16 at 22:51
  • can you test the new one and report the result? – Seyed Ali Roshan Nov 06 '16 at 22:54
  • I tried the updated code and still received the same error `Error: org.json.JSONException: JSONObject["related"] not found`. Here's a [gist](https://gist.github.com/anonymous/9ec90dff79e824361ea5892aa9b936b5) of the code I used. – lemon master Nov 06 '16 at 23:08
  • you have a problem on gist. in line 27: change JSONArray to JSONObject, and in the next line act like my code, JSONArray boutThogether = related.getJSONArray("bought_together"); – Seyed Ali Roshan Nov 07 '16 at 00:27
  • Oops, I posted the wrong code on the gist. However, on further inspection it seem the problem wasn't ever in the code, it was in the sample dataset I was using. I used -tail to create a sample dataset to test on and none of the test objects contained the json key 'related' because they had no related products. When testing, I removed my try catch block(json exception) to see what json exception errors I was receiving, so normally these would've been skipped over. – lemon master Nov 07 '16 at 00:46