Recursively list all object property paths from JSON [duplicate]

点点圈 提交于 2019-12-01 01:59:18

If you truly only need those two values, you can do it while it's being parsed.

var data = {};

// ----------v--- Original JSON data before parsing.
JSON.parse(myJSON, function(k, v) {
    if (k === "AmazonOrderId" || k === "OrderItem") {
        data[k] = v;
    }
    return v;
});

That said... recursive stuff shouldn't be scary. And there are many pre-written functions on StackOverflow that accept the property you're looking for and return the value, so you could just use one of those.


Here's a simple recursive function that generates paths to all the data.

function objectToPaths(data) {
  var validId = /^[a-z_$][a-z0-9_$]*$/i;
  var result = [];
  doIt(data, "");
  return result;

  function doIt(data, s) {
    if (data && typeof data === "object") {
      if (Array.isArray(data)) {
        for (var i = 0; i < data.length; i++) {
          doIt(data[i], s + "[" + i + "]");
        }
      } else {
        for (var p in data) {
          if (validId.test(p)) {
            doIt(data[p], s + "." + p);
          } else {
            doIt(data[p], s + "[\"" + p + "\"]");
          }
        }
      }
    } else {
      result.push(s);
    }
  }
}

All Array members are listed individually. This could be consolidated if you know that the data structures in those array members are always identical.

Result for your data:

.ListOrderItemsResponse.$.xmlns
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].QuantityOrdered[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].Title[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ShippingTax[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ShippingTax[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].PromotionDiscount[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].PromotionDiscount[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ConditionId[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ASIN[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].SellerSKU[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].OrderItemId[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].GiftWrapTax[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].GiftWrapTax[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].QuantityShipped[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ShippingPrice[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ShippingPrice[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].GiftWrapPrice[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].GiftWrapPrice[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ConditionSubtypeId[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ItemPrice[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ItemPrice[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ItemTax[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ItemTax[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ShippingDiscount[0].CurrencyCode[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].OrderItems[0].OrderItem[0].ShippingDiscount[0].Amount[0]
.ListOrderItemsResponse.ListOrderItemsResult[0].AmazonOrderId[0]
.ListOrderItemsResponse.ResponseMetadata[0].RequestId[0]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!