0

I am very new to jQuery, and am trying to figure out how to put nested JSON Objects into an array. Here is the JSON data:

{
  "0": {
    "country": "US",
    "itemId": "391296746967",
    "price": "4.99",
    "shippingCost": {
      "expeditedShipping": "false",
      "handlingTime": "1",
      "oneDayShippingAvailable": "false",
      "shipToLocations": "Worldwide",
      "shippingServiceCost": {
        "_currencyId": "USD",
        "value": "0.0"
      },
      "shippingType": "Free"
    },
    "title": "Tea Infuser Ball Mesh Loose Leaf Herb Strainer Stainless Steel Secure Locking 2\"",
    "user_args": {
      "advanced": null,
      "pages": {
        "entries_per_page": 4,
        "page_number": 4
      },
      "search_terms": "ball"
    }
  },
  "1": {
    "country": "US",
    "itemId": "382548457911",
    "price": "18.9",
    "shippingCost": {
      "expeditedShipping": "true",
      "handlingTime": "1",
      "oneDayShippingAvailable": "false",
      "shipToLocations": "Worldwide",
      "shippingServiceCost": {
        "_currencyId": "USD",
        "value": "0.0"
      },
      "shippingType": "Free"
    },
    "title": "Baby Kid Outdoor Indoor Princess Play Tent Playhouse Ball Pit Pool Toddler Toys",
    "user_args": {
      "advanced": null,
      "pages": {
        "entries_per_page": 4,
        "page_number": 4
      },
      "search_terms": "ball"
    }
  },
  "2": {
    "country": "US",
    "itemId": "132543955826",
    "price": "13.99",
    "shippingCost": {
      "expeditedShipping": "false",
      "handlingTime": "1",
      "oneDayShippingAvailable": "false",
      "shipToLocations": "Worldwide",
      "shippingServiceCost": {
        "_currencyId": "USD",
        "value": "0.0"
      },
      "shippingType": "Free"
    },
    "title": "Yoga Ball w Air Pump Anti Burst Exercise Balance Workout Stability 55 65 75 85cm",
    "user_args": {
      "advanced": null,
      "pages": {
        "entries_per_page": 4,
        "page_number": 4
      },
      "search_terms": "ball"
    }
  },
  "3": {
    "country": "US",
    "itemId": "173659697373",
    "price": "155.0",
    "shippingCost": {
      "expeditedShipping": "false",
      "handlingTime": "0",
      "oneDayShippingAvailable": "false",
      "shipToLocations": "Worldwide",
      "shippingServiceCost": {
        "_currencyId": "USD",
        "value": "0.0"
      },
      "shippingType": "Free"
    },
    "title": "DribbleUp Smart Soccer Ball with Training App Size 5 For Adults (***Buy ME***)",
    "user_args": {
      "advanced": null,
      "pages": {
        "entries_per_page": 4,
        "page_number": 4
      },
      "search_terms": "ball"
    }
  }
}

From this JSON I need the price, the shippingCost.value, the shippingType, and the title. I have searched for ways to do this, but all I find is information about the $.getJSON() method, and I can't figure out how to use that to achieve my goal. If anyone could point me in the right direction, it would be greatly appreciated!

Tom O.
  • 5,730
  • 2
  • 21
  • 35
jtetra13
  • 1
  • 7
  • This is not a JSON array. This is a JSON Object with properties that are themselves other JSON Objects. Compare what you have with what is shown [here](https://www.json.org/), under arrays. Or better yet, visualize it [here](http://jsonviewer.stack.hu/). – cosh Nov 29 '18 at 16:55
  • Your data doesn't look like [JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON), nor it doesn't look like a JS object, or even a JSON string ... Please elaborate the question. What do you have, where it is coming from and how, and in which form? Please also take some time, and read carefully some answers on [this SO question](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). – Teemu Nov 29 '18 at 16:57
  • We definitely need more information. Your title and code suggests you're trying to push a nested object(not JSON) into an array, but your question seems to be that you're trying to retrieve value data? please elaborate. – zfrisch Nov 29 '18 at 16:59
  • I'm sorry, I really have no idea what I'm doing. The JSON data comes from this URL, which generates search results: http://hadrians-search.tk/search?search_param=ball&items_per_page=4&page_number=4 – jtetra13 Nov 29 '18 at 17:13
  • Have you already print that in console? – eag845 Nov 29 '18 at 17:17
  • JSON means JavaScript Object Notation; it's just a JavaScript object that has been serialized to text (for instance to communicate it over http as in your context). `$.getJSON("http://hadrians-search.tk/search?search_param=ball&items_per_page=4&page_number=4')` will get you the javascript object the JSON served by the url represents. From there it's just standard JS object manipulation, e.g. `result["1"].country` should yield "US" (using `["1"]` instead of `.1` because `1` isn't a valid field name ; bad JSON, bad !) – Aaron Nov 29 '18 at 17:18

2 Answers2

0

You can get the JSON using $.getJSON, then iterate over their values and push them into an array.

Try this http://jsfiddle.net/j26bkomu/ (open the console)

eag845
  • 1,013
  • 1
  • 11
  • 16
0

Once you have the json you can iterate over its key value pairs and save them in an array:

$.getJSON( "./test.json", function( data ) {
  var items = [];
  var objJson = {};

  $.each( data, function( key, val ) {

      objJson.price = val.price;
      objJson.value = val.shippingCost.shippingServiceCost.value;
      objJson.type = val.shippingCost.shippingType;
      objJson.title = val.title;

    items.push( objJson );
    objJson = {};
  });

  console.log(items);


});

output:

(4) [{…}, {…}, {…}, {…}]
0:
price: "4.99"
title: "Tea Infuser Ball Mesh Loose Leaf Herb Strainer Stainless Steel Secure Locking 2""
type: "Free"
value: "0.0"
__proto__: Object
1:
price: "18.9"
title: "Baby Kid Outdoor Indoor Princess Play Tent Playhouse Ball Pit Pool Toddler Toys"
type: "Free"
value: "0.0"
__proto__: Object
2: {price: "13.99", value: "0.0", type: "Free", title: "Yoga Ball w Air Pump Anti Burst Exercise Balance Workout Stability 55 65 75 85cm"}
3: {price: "155.0", value: "0.0", type: "Free", title: "DribbleUp Smart Soccer Ball with Training App Size 5 For Adults (***Buy ME***)"}
length: 4
__proto__: Array(0)
Mario L
  • 224
  • 1
  • 11