-2

I got this JSON data as below and want to get the ip_address and '5865cdc5-0f8c-481b-aaa7-4adfe6cf96ae' can be any string.

history': {
    '5865cdc5-0f8c-481b-aaa7-4adfe6cf96ae': {
      'profile': [
        {
          'category': 'Linux',
          'time_stamp': 1489120439.877187,
          'detected_time': '2017-03-10T04:33:59Z',
          'os': 'Linux',
          'collector_type': 'dhcp'
        }
      ],
      'ip_address': [
        {
          'ip': '10.204.49.218',
          'detected_time': '2017-03-10T04:33:59Z',
          'hostname': 'pxe-dev',
          'collector_type': 'dhcp',
          'time_stamp': 1489120439.875652
        }
      ]
    }
  }

I want to get the 'ip_address' array. Any help please

Kim
  • 315
  • 4
  • 7
  • 13

1 Answers1

2

that is not JSON but normal 'semi' Object instead Use

    //and dont use *history* var as it conflicted with global variable
    obj = {
    '5865cdc5-0f8c-481b-aaa7-4adfe6cf96ae': {
      'profile': [
        {
          'category': 'Linux',
          'time_stamp': 1489120439.877187,
          'detected_time': '2017-03-10T04:33:59Z',
          'os': 'Linux',
          'collector_type': 'dhcp'
        }
      ],
      'ip_address': [
        {
          'ip': '10.204.49.218',
          'detected_time': '2017-03-10T04:33:59Z',
          'hostname': 'pxe-dev',
          'collector_type': 'dhcp',
          'time_stamp': 1489120439.875652
        }
      ]
    }
  }

var ip = obj['5865cdc5-0f8c-481b-aaa7-4adfe6cf96ae']['ip_address'][0]['ip'])

or

var ipAddress = obj['5865cdc5-0f8c-481b-aaa7-4adfe6cf96ae']['ip_address'])

if you want to check the JSON, please check following and before getting vale you need to parse it back to Javascript object

obj = {
    '5865cdc5-0f8c-481b-aaa7-4adfe6cf96ae': {
      'profile': [
        {
          'category': 'Linux',
          'time_stamp': 1489120439.877187,
          'detected_time': '2017-03-10T04:33:59Z',
          'os': 'Linux',
          'collector_type': 'dhcp'
        }
      ],
      'ip_address': [
        {
          'ip': '10.204.49.218',
          'detected_time': '2017-03-10T04:33:59Z',
          'hostname': 'pxe-dev',
          'collector_type': 'dhcp',
          'time_stamp': 1489120439.875652
        }
      ]
    }
  }

var t = JSON.stringify(obj)
var t2 = JSON.parse(t)
console.log('json:', t)
console.log('javascript object', t2)
RizkiDPrast
  • 1,695
  • 1
  • 14
  • 21