0

I want to sort a JSON array based on time value in a subarray with the key names of the subarrays being named uniquely.

I'm searching for the method to access key, value update_time of every element in Products so I can use that value in a sorting script.

I have tried sorting the array but can not determine how to access the key, values of the subarrays

Expected behavior should be that every unique_keyname_# element is available for sorting and is sorted for further processing in JavaScript. Ultimately with the newest unique_keyname_# as the first element in a list, based on the update_time key.

var obj = {
  "company": {
    "department_1": {
      "Products": {
        "unique_keyname_1": {
          "product_owner": "co-worker-1",
          "update_time": "unix_timestamp_1"
        },
        "unique_keyname_5": {
          "product_owner": "co-worker-4",
          "update_time": "unix_timestamp_45"
        },
        "unique_keyname_8": {
          "product_owner": "co-worker-2",
          "update_time": "unix_timestamp_5"
        }
      }
    },
    "department_2": {
      "Products": {
        "unique_keyname_3": {
          "product_owner": "co-worker-1",
          "update_time": "unix_timestamp_21"
        },
        "unique_keyname_6": {
          "product_owner": "co-worker-2",
          "update_time": "unix_timestamp_7"
        },
        "unique_keyname_4": {
          "product_owner": "co-worker-3",
          "update_time": "unix_timestamp_75"
        }
      }
    }
  }
}
Kobe
  • 6,226
  • 1
  • 14
  • 35
  • 1
    There is no array in your JSON?! – Marc Aug 19 '19 at 08:39
  • 1
    Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan Aug 19 '19 at 08:40
  • 3
    Do you have control over how that JSON is generated? The structure is pretty illogical, something like this would be easier to traverse: `{"company": { "departments: [{ Products: [{ "product_owner": "foo" }] }] } }` – Cerbrus Aug 19 '19 at 08:41
  • 1
    Don't snippet code that doesn't run. If the OP adds what he attempted, we can always convert it to a snippet. – Cerbrus Aug 19 '19 at 08:42
  • @cerbrus Thanks for your response. I do not have control over the generated JSON. This is a thrid party response ro a GET request. I will attempt to rewrite the response with some code into a more appropiate response. I was already doubting the structure logic and that put off finding a good way to work with it. Thanks for establishing my doubt as valid! – Ruudvddries Aug 19 '19 at 08:48

2 Answers2

0

I solved the issue by writing an intermediate script in python which makes the API response a valid array. From there it was fairly easy to sort the data.

Thanks for the replies confirming the data itself was deliverd to me in an inappropriate format!

regards

-2

In your example, there are no arrays. Anyway, in Javascript you can access a node using . like:

 obj.company.department_1.Products.unique_keyname_1

Or using [] which gives you more freedom to use costume fields

 obj["company"]["department_1"]["Products"]["unique_keyname_1"]
// can also be more dynamic as:
 obj["company"]["department_"+ department_counter]["Products"]["unique_keyname_" + keyname_counter]

Is there a possibility that you will change the structure of your JSON? to make it more manangeable ?

if so, i would recommend the folowing structure:

var products = [
  {
    department: 'SomeDepartment',
    productName: 'Something',
    productOwner: 'Someone',
    update_time: 'Sometime'
  }
]

Then you can sort the array easy using Array.sort() for the sort topic use this : Sort array of objects by string property value

Gugu
  • 193
  • 1
  • 8
  • 1
    This solution relies on the amount of departments being known. – Cerbrus Aug 19 '19 at 09:05
  • Thank you for your comment @Cerbrus. But - it depends only on the products. Then you can sort by departments, and sub-sort by whatever. – Gugu Aug 19 '19 at 10:12
  • Did you see the key names for products? `unique_keyname_3`, `unique_keyname_6`, `unique_keyname_4`... If you don't know these Ids before-hand, there's not much you can do with this answer. – Cerbrus Aug 19 '19 at 10:14