Python 3 - Extracting value from key in nested dictionary - json

Hoping for some pointers here as I'm striking out with my attempts. Am working with python 3.8.5.
I'm querying a Car Park booking system that returns a json list of availability. The result has lots of nested dictionaries and I'm struggling to extract just the values i want.
This is what I've been doing:
import requests
import json
enquiry = requests.post(url.....) #queries api, this works fine
results = enquiry.text #extracts response data
dictionary = json.loads(results) #convert response to python dict
If there is one slot available, I get this output (apologies for length). If there are multiple slots available, i get the same output repeated:
{
"data": {
"services": [{
"id": null,
"name": null,
"services": [{
"id": null,
"name": "Car Park",
"met": true,
"filterCount": 1,
"primary": true,
"options": [{
"id": "9",
"images": [],
"available": true,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": null,
"startDate": "2020-08-18T13:30:00Z", <--this is what I want to extract
"endDate": "2020-08-18T14:30:00Z",
"geo": {
"lat": 0.0,
"lng": 0.0
},
"selected": false,
"linkedServices": [],
"tiers": null
}]
}],
"currentBookingId": null,
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
},
"endDate": {
"ms": 1597761000000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 15,
"days": 0
}
},
"sessionId": "2222222",
"chargeType": 1,
"hasPrimaryBookable": false,
"hasBookable": false,
"hasDiscounts": false,
"hasMultipleTiers": false,
"isPreferred": false,
"primaryServiceAvailable": true,
"primaryServiceId": null,
"primaryServiceType": "undefined",
"unavailableAttendees": []
}],
"bookingLimit": null
},
"success": true,
"suppress": false,
"version": "2.3.293",
"message": null,
"result": null,
"errors": null,
"code": null,
"flags": 0,
"redirect": null
}
I want to extract:
"startDate": "2020-08-18T13:30:00Z"
from each key, value pair in any of the returned slots. However, I cant work it out.
Extracting the whole of this nested dictionary would also contain the same data, but would then involve more work to tidy it up after.
"startDate": {
"ms": 1597757400000,
"year": 2020,
"month": 8,
"day": 18,
"dayOfWeek": 2,
"time": {
"seconds": 0,
"minutes": 30,
"hours": 14,
"days": 0
}
I've tried loads of dictionary.get and dictionary.items variations, but cant seem to get anywhere.
I tried something like
key = ('startDate')
availability = dictionary.get(key)
print(availability)
this just returns 'none', so think im way off
Any pointers?
Thanks in advance!

Thanks for the full data. It makes testing easier :)
I had to replace null -> None, true -> True, false -> False
slot = {
"data": {
"services": [{
"id": None,
"name": None,
"services": [{
"id": None,
"name": "Car Park",
"met": True,
"filterCount": 1,
"primary": True,
"options": [{
"id": "9",
"images": [],
"available": True,
"calendarId": "AAAA",
"templateId": "BBBB",
"capacity": 0,
"name": "Car Park Slot 3",
"sessionId": "CCCC",
"functions": None,
"startDate": "2020-08-18T13:30:00Z", # <--this is what I want to extract
................
print("Start Date:", slot['data']['services'][0]['services'][0]['options'][0]['startDate'])
Output
Start Date: 2020-08-18T13:30:00Z

Related

How do I get back the value from one of the nested dictionary from JSON file

i am new to python and json. I have this issue where i am unable to get the values of 'labels' print out. It is under a nested dictionary 'result' .
Code that i have tried
for i in data['result']:
print("Value:", i['value'])
values = i['value']
print("X:", values['x'])
print("Y:", values['y'])
print("Width:", values['width'])
print("Height:", values['height'])
Output from the above code
However when i try to do print("labels:", values['labels'])
it return a keyerror: 'labels'
for item in values:
print(item)
this is the output shown
x
y
width
height
rotation
x
y
width
height
rotation
labels
May i ask how do i go out to print out the 'labels' value?
The json files is as follows:
{
"id": 9,
"created_username": "",
"created_ago": "3\u00a0weeks, 3\u00a0days",
"completed_by": {
"id": 1,
"first_name": "",
"last_name": "",
"email": ""
},
"task": {
"id": 1,
"data": {
"image": "/data/upload/2/adf8540d-2-33.png"
},
"meta": {},
"created_at": "2022-11-21T14:52:22.478537Z",
"updated_at": "2022-11-22T12:51:18.105745Z",
"is_labeled": true,
"overlap": 1,
"inner_id": 1,
"total_annotations": 1,
"cancelled_annotations": 0,
"total_predictions": 0,
"comment_count": 0,
"unresolved_comment_count": 0,
"last_comment_updated_at": null,
"project": 2,
"updated_by": 1,
"file_upload": 70,
"comment_authors": []
},
"result": [
{
"original_width": 512,
"original_height": 512,
"image_rotation": 0,
"value": {
"x": 50.898958419872464,
"y": 44.0069438675168,
"width": 2.397222452993284,
"height": 2.0975696463691196,
"rotation": 0
},
"id": "pgaiV8NjWC",
"from_name": "rect",
"to_name": "image",
"type": "rectangle",
"origin": "manual"
},
{
"original_width": 512,
"original_height": 512,
"image_rotation": 0,
"value": {
"x": 50.898958419872464,
"y": 44.0069438675168,
"width": 2.397222452993284,
"height": 2.0975696463691196,
"rotation": 0,
"labels" [
"LM"*
]
},
"id": "pgaiV8NjWC",
"from_name": "labels",
"to_name": "image",
"type": "labels",
"origin": "manual"
}
],
"was_cancelled": false,
"ground_truth": false,
"created_at": "2022-11-22T12:51:18.001777Z",
"updated_at": "2022-11-22T12:51:18.001777Z",
"lead_time": 21.556,
"project": 2,
"parent_prediction": null,
"parent_annotation": null
}
Use the json module
import json
dict = json.load(json_filename)
Then you should access the values by the dictionary keys. Be aware that the result value is a list. Therefore the index 1 is necessary.
values = dict['result'][1]['value']
print("X:", values['x'])

json jq request format

I need help to extract data from this JSON file using jq.
The app flv then verify the streamname mystrame is active and extract meta.video.height
I did try lot of queries without success and my jq knowledge is poor.
{
"port": 1935,
"server_index": 0,
"applications": [{
"name": "hls",
"live": {
"streams": [{
"name": "donbosco",
"time": 2380739,
"bw_in": 2112440,
"bytes_in": 541618713,
"bw_out": 0,
"bytes_out": 0,
"bw_audio": 35544,
"bw_video": 2076888,
"clients": [{
"id": 453,
"address": "127.0.0.1",
"time": 2380959,
"flashver": "FMLE/3.0 (compatible; Lavf57.83.100)",
"dropped": 0,
"avsync": 28,
"timestamp": 2382635,
"publishing": true,
"active": true
}],
"records": [],
"meta": {
"video": {
"width": 1168,
"height": 720,
"frame_rate": 25,
"codec": "H264",
"profile": "High",
"level": 3.1
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": 2,
"sample_rate": 16000
}
},
"nclients": 1,
"publishing": true,
"active": true
}],
"nclients": 1
},
"recorders": {
"count": 0,
"lists": []
}
},
{
"name": "flv",
"live": {
"streams": [{
"name": "mystream",
"time": 2382811,
"bw_in": 2059096,
"bytes_in": 541841549,
"bw_out": 2059096,
"bytes_out": 543351459,
"bw_audio": 35472,
"bw_video": 2023624,
"clients": [{
"id": 452,
"address": "127.0.0.1",
"time": 2382727,
"flashver": "LNX 9,0,124,2",
"dropped": 0,
"avsync": -12,
"timestamp": 2384520,
"publishing": false,
"active": true
},
{
"id": 451,
"address": "127.0.0.1",
"time": 2383031,
"flashver": "FMLE/3.0 (compatible; Lavf58.74.100)",
"dropped": 0,
"avsync": -12,
"timestamp": 2384520,
"publishing": true,
"active": true
}
],
"records": [],
"meta": {
"video": {
"width": 1168,
"height": 720,
"frame_rate": 25,
"codec": "H264",
"profile": "High",
"level": 3.1
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": 2,
"sample_rate": 16000
}
},
"nclients": 2,
"publishing": true,
"active": true
}],
"nclients": 2
},
"recorders": {
"count": 0,
"lists": []
}
}
]
}
Are you asking for help with the command-line JSON processor jq or the JavaScript library jQuery? They are not the same. For jq, try
jq '
.applications
| map(select(.name == "flv"))[].live.streams
| map(select(.name == "mystream" and .active))[].meta.video.height
'
720
Demo

How to fetch multiple values from a nested JSON like output

I am looking forward a way to list the value from a nested variable (which has dict and list values) ie register which is response_find and I have that in mostly json format and most of the values inside the json are nested within [] list like construct.
It works somehow if I get individual values from it like below:
var=response_find['json']['results'][0]['content_facet_attributes']
or
var=response_find['json']['results'][0]['certname']
But did not work when I did below:
var=response_find['json']['results'][0]['content_view_id']
Below are few values which I'm looking forward to fetch from this nested output:
"architecture_name": "x86_64",
"name": "satcap.rest.example.com",
"url": "https://satcap.rest.example.com:9090"
"content_source_name": "satcap.rest.example.com",
"name": "ccv-azure-infra-rhel7"
"name": "Prod"
"id": 33485,
"ip": "192.168.88.88",
"name": "invwharn108.test.exampl.com",
"registered_through": "satcap.rest.example.com"
Nested Json output:
{
"response_find": {
"apipie_checksum": "7533ab625c45a3819647f4fb4c9394c2832c0180",
"cookies": {
"_session_id": "25872833d9e2723073797fbd8cfa2d63"
},
"cookies_string": "_session_id=25872833d9e2723073797fbd8cfa2d63",
"foreman_version": "1.24.1.21",
"json": {
"page": 1,
"per_page": 20,
"results": [
{
"architecture_id": 1,
"architecture_name": "x86_64",
"build": false,
"capabilities": [
"build"
],
"certname": "invwharn108.test.exampl.com",
"comment": null,
"content_facet_attributes": {
"applicable_module_stream_count": 0,
"applicable_package_count": 0,
"content_source": {
"id": 17,
"name": "satcap.rest.example.com",
"url": "https://satcap.rest.example.com:9090"
},
"content_source_id": 17,
"content_source_name": "satcap.rest.example.com",
"content_view": {
"id": 67,
"name": "ccv-azure-infra-rhel7"
},
"content_view_id": 67,
"content_view_name": "ccv-azure-infra-rhel7",
"errata_counts": {
"bugfix": 0,
"enhancement": 0,
"security": 0,
"total": 0
},
"id": 32255,
"kickstart_repository": {
"id": 5772,
"name": "Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.8"
},
"kickstart_repository_id": 5772,
"kickstart_repository_name": "5772",
"lifecycle_environment": {
"id": 5,
"name": "Prod"
},
"lifecycle_environment_id": 5,
"lifecycle_environment_name": "Prod",
"upgradable_module_stream_count": 0,
"upgradable_package_count": 0,
"uuid": "dab2d66b-1a73-490a-aa9d-3f036658980a"
},
"created_at": "2020-12-23 13:19:35 UTC",
"disk": null,
"domain_id": 25,
"domain_name": "test.example-aws.example.com",
"enabled": true,
"environment_id": null,
"environment_name": null,
"errata_status": 1,
"errata_status_label": "Could not calculate errata status, ensure host is registered and the katello-host-tools package is installed",
"global_status": 1,
"global_status_label": "Warning",
"hostgroup_id": 982,
"hostgroup_name": "infra",
"hostgroup_title": "NXDI-hg-aws/west-europe/infra",
"id": 33485,
"image_file": "",
"image_id": null,
"image_name": null,
"installed_at": null,
"ip": "192.168.88.88",
"ip6": null,
"last_compile": "2020-12-23 13:19:38 UTC",
"last_report": null,
"location_id": 37,
"location_name": "west-europe",
"mac": "02:78:43:60:ee:fb",
"managed": false,
"medium_id": null,
"medium_name": null,
"model_id": 25,
"model_name": "HVM domU",
"name": "invwharn108.test.exampl.com",
"openscap_proxy": null,
"openscap_proxy_id": null,
"openscap_proxy_name": null,
"operatingsystem_id": 21,
"operatingsystem_name": "RedHat 7.8",
"organization_id": 3,
"pxe_loader": "PXELinux BIOS",
"subnet_id": 65,
"subnet_name": "aws-west-europe-infra",
"subscription_facet_attributes": {
"autoheal": true,
"hypervisor": false,
"id": 33593,
"last_checkin": "2020-12-23 13:19:42 UTC",
"purpose_addons": [],
"purpose_role": "",
"purpose_usage": "",
"registered_at": "2020-12-23 13:19:35 UTC",
"registered_through": "satcap.rest.example.com",
},
"subscription_global_status": 1,
"subscription_status": 1,
"subscription_status_label": "Partially entitled",
}
],
"search": "name=invwharn108.test.exampl.com",
"sort": {
"by": null,
"order": null
},
"subtotal": 1,
"total": 13121
},
"msg": "OK (unknown bytes)",
"x_request_id": "e02dd9b3-b2f8-4c39-8bf5-1ce1897d9548",
}
}
My Play:
---
- hosts: localhost
tasks:
- include_vars: vaults/aws_secrets.yml
no_log: true
- include_vars: requirements.yml
no_log: true
- name: Find the ID of Cloud VM in Satellite
uri:
url: "https://{{ satserver }}/api/v2/hosts?search=name={{ aws_instance_name }}"
method: GET
user: "{{ aws_satuser }}"
password: "{{ aws_satpw }}"
force_basic_auth: yes
validate_certs: no
register: response_find
delegate_to: localhost
changed_when: false
- debug:
var=response_find['json']['results'][0]['content_facet_attributes']
#var=response_find.json.results.0.organization_name
#var=response_find['json']['results'][0]['certname']
Result:
"content_facet_attributes": {
"applicable_module_stream_count": 0,
"applicable_package_count": 0,
"content_source": {
"id": 17,
"name": "satcap.rest.example.com",
"url": "https://satcap.rest.example.com:9090"
},
"content_source_id": 17,
"content_source_name": "satcap.rest.example.com",
"content_view": {
"id": 67,
"name": "ccv-azure-infra-rhel7"
},
"content_view_id": 67,
"content_view_name": "ccv-azure-infra-rhel7",
"errata_counts": {
"bugfix": 0,
"enhancement": 0,
"security": 0,
"total": 0
},
"id": 32255,
"kickstart_repository": {
"id": 5772,
"name": "Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.8"
},
"kickstart_repository_id": 5772,
"kickstart_repository_name": "5772",
"lifecycle_environment": {
"id": 5,
"name": "Prod"
},
"lifecycle_environment_id": 5,
"lifecycle_environment_name": "Prod",
"upgradable_module_stream_count": 0,
"upgradable_package_count": 0,
"uuid": "dab2d66b-1a73-490a-aa9d-3f036658980a"
},
If you want to do a dictionary out of those data you listed, you could use the filter json_query, which is using JMESPath to parse and process JSON.
In order to extract a dictionary looking like:
{
"architecture_name": "x86_64",
"content_source_name": "satcap.rest.example.com",
"content_source_url": "https://satcap.rest.example.com:9090",
"content_view_name": "ccv-azure-infra-rhel7",
"egistered_through": "satcap.rest.example.com",
"id": 33485,
"ip": "192.168.88.88",
"lifecycle_environment_name": "Prod",
"name": "invwharn108.test.exampl.com"
}
You could use JMESPath to filter multiselect hashes.
Here is a JMESPath query that would give this result:
json.results[*].{
"architecture_name": architecture_name,
"content_source_name": content_facet_attributes.content_source.name,
"content_source_url": content_facet_attributes.content_source.url,
"content_view_name": content_facet_attributes.content_view.name,
"lifecycle_environment_name": content_facet_attributes.lifecycle_environment.name,
"id": id,
"ip": ip,
"name": name,
"egistered_through": subscription_facet_attributes.registered_through
}
Here is an example playbook using this:
- hosts: localhost
gather_facts: no
tasks:
- debug:
msg: >-
{{
(
response_find | json_query('
json.results[*].{
"architecture_name": architecture_name,
"content_source_name": content_facet_attributes.content_source.name,
"content_source_url": content_facet_attributes.content_source.url,
"content_view_name": content_facet_attributes.content_view.name,
"lifecycle_environment_name": content_facet_attributes.lifecycle_environment.name,
"id": id,
"ip": ip,
"name": name,
"egistered_through": subscription_facet_attributes.registered_through
}
')
).0
}}
vars:
{
"response_find": {
"apipie_checksum": "7533ab625c45a3819647f4fb4c9394c2832c0180",
"cookies": {
"_session_id": "25872833d9e2723073797fbd8cfa2d63"
},
"cookies_string": "_session_id=25872833d9e2723073797fbd8cfa2d63",
"foreman_version": "1.24.1.21",
"json": {
"page": 1,
"per_page": 20,
"results": [{
"architecture_id": 1,
"architecture_name": "x86_64",
"build": false,
"capabilities": [
"build"
],
"certname": "invwharn108.test.exampl.com",
"comment": null,
"content_facet_attributes": {
"applicable_module_stream_count": 0,
"applicable_package_count": 0,
"content_source": {
"id": 17,
"name": "satcap.rest.example.com",
"url": "https://satcap.rest.example.com:9090"
},
"content_source_id": 17,
"content_source_name": "satcap.rest.example.com",
"content_view": {
"id": 67,
"name": "ccv-azure-infra-rhel7"
},
"content_view_id": 67,
"content_view_name": "ccv-azure-infra-rhel7",
"errata_counts": {
"bugfix": 0,
"enhancement": 0,
"security": 0,
"total": 0
},
"id": 32255,
"kickstart_repository": {
"id": 5772,
"name": "Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.8"
},
"kickstart_repository_id": 5772,
"kickstart_repository_name": "5772",
"lifecycle_environment": {
"id": 5,
"name": "Prod"
},
"lifecycle_environment_id": 5,
"lifecycle_environment_name": "Prod",
"upgradable_module_stream_count": 0,
"upgradable_package_count": 0,
"uuid": "dab2d66b-1a73-490a-aa9d-3f036658980a"
},
"created_at": "2020-12-23 13:19:35 UTC",
"disk": null,
"domain_id": 25,
"domain_name": "test.example-aws.example.com",
"enabled": true,
"environment_id": null,
"environment_name": null,
"errata_status": 1,
"errata_status_label": "Could not calculate errata status, ensure host is registered and the katello-host-tools package is installed",
"global_status": 1,
"global_status_label": "Warning",
"hostgroup_id": 982,
"hostgroup_name": "infra",
"hostgroup_title": "NXDI-hg-aws/west-europe/infra",
"id": 33485,
"image_file": "",
"image_id": null,
"image_name": null,
"installed_at": null,
"ip": "192.168.88.88",
"ip6": null,
"last_compile": "2020-12-23 13:19:38 UTC",
"last_report": null,
"location_id": 37,
"location_name": "west-europe",
"mac": "02:78:43:60:ee:fb",
"managed": false,
"medium_id": null,
"medium_name": null,
"model_id": 25,
"model_name": "HVM domU",
"name": "invwharn108.test.exampl.com",
"openscap_proxy": null,
"openscap_proxy_id": null,
"openscap_proxy_name": null,
"operatingsystem_id": 21,
"operatingsystem_name": "RedHat 7.8",
"organization_id": 3,
"pxe_loader": "PXELinux BIOS",
"subnet_id": 65,
"subnet_name": "aws-west-europe-infra",
"subscription_facet_attributes": {
"autoheal": true,
"hypervisor": false,
"id": 33593,
"last_checkin": "2020-12-23 13:19:42 UTC",
"purpose_addons": [],
"purpose_role": "",
"purpose_usage": "",
"registered_at": "2020-12-23 13:19:35 UTC",
"registered_through": "satcap.rest.example.com"
},
"subscription_global_status": 1,
"subscription_status": 1,
"subscription_status_label": "Partially entitled"
}],
"search": "name=invwharn108.test.exampl.com",
"sort": {
"by": null,
"order": null
},
"subtotal": 1,
"total": 13121
},
"msg": "OK (unknown bytes)",
"x_request_id": "e02dd9b3-b2f8-4c39-8bf5-1ce1897d9548"
}
}
Which gives the recap:
PLAY [localhost] *************************************************************************************************
TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"msg": {
"architecture_name": "x86_64",
"content_source_name": "satcap.rest.example.com",
"content_source_url": "https://satcap.rest.example.com:9090",
"content_view_name": "ccv-azure-infra-rhel7",
"egistered_through": "satcap.rest.example.com",
"id": 33485,
"ip": "192.168.88.88",
"lifecycle_environment_name": "Prod",
"name": "invwharn108.test.exampl.com"
}
}
PLAY RECAP *******************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

How to extract multiple correlating variables from a JSon

I have to extract multiple correlating variables from a response (which is json) in JMeter. Part of the response is listed below:
[
{
"data": {
"id": "efaa6876-7a8d-4723-9d85-1ed99e822f06",
"type": "courses",
"attributes": {
"created-at": "2019-02-07T16:38:50.735Z",
"contents-count": 267,
"units": [
{
"id": "31b5fcb1-24ee-441e-a0ee-ca859fc9a89d",
"position": null,
"progress": 0,
"completed": false,
"show_name": false,
"node_id": "1",
"children": [
{
"id": "b8ed75a3-0390-4273-82c3-03ee6eba729c",
"position": null,
"image": null,
"progress": 0,
"completed": false,
"show_name": true,
"node_id": "2",
"children": [],
"contents": [
{
"id": "fa1bdc2f-4330-425c-9c10-3734d07125aa",
"link": {
"url": "#",
"target": "_blank",
"class": "learning-object-link",
"data": {
"id": "fa1bdc2f-4330-425c-9c10-3734d07125aa",
"user-role": "teacher",
"open-method-tablet": "newtab",
"open-method-desktop": "modal",
"content-open-method": null,
"modal-size-method": "fully_responsive",
"fixed-width": null,
"fixed-height": null,
"aspect-ratio": null
}
},
"is_work": false,
"is_fun": false,
"completed": false,
"total_activities": 2,
"completed_activities": 0,
"progress": 0,
"updated_at": false,
"attempts": 0,
"duration": null
},
{
"id": "ceceabfd-5151-4656-af5d-3392c5a4c04c",
"link": {
"url": "#",
"target": "_blank",
"class": "learning-object-link",
"data": {
"id": "ceceabfd-5151-4656-af5d-3392c5a4c04c",
"user-role": "teacher",
"open-method-tablet": "newtab",
"open-method-desktop": "modal",
"content-open-method": null,
"modal-size-method": "fully_responsive",
"fixed-width": null,
"fixed-height": null,
"aspect-ratio": null
}
},
"is_work": false,
"is_fun": false,
"completed": false,
"total_activities": 2,
"completed_activities": 0,
"progress": 0,
"updated_at": false,
"attempts": 0,
"duration": null
}
]
},
{
"id": "60639cbd-f872-492d-b8e9-db83f8789fcf",
"position": null,
"image": null,
"progress": 0,
"completed": false,
"show_name": true,
"node_id": "3",
"children": [],
"contents": [
{
"id": "1825f834-7099-4bb4-b7a2-fc634faffc86",
"link": {
"url": "#",
"target": "_blank",
"class": "learning-object-link",
"data": {
"id": "1825f834-7099-4bb4-b7a2-fc634faffc86",
"user-role": "teacher",
"open-method-tablet": "newtab",
"open-method-desktop": "modal",
"content-open-method": null,
"modal-size-method": "fully_responsive",
"fixed-width": null,
"fixed-height": null,
"aspect-ratio": null
}
},
To proceed with the next request, I have to extract unit id (e.g. 31b5fcb1-24ee-441e-a0ee-ca859fc9a89d), children id (e.g. b8ed75a3-0390-4273-82c3-03ee6eba729c) and contents id (e.g. fa1bdc2f-4330-425c-9c10-3734d07125aa). There are several units, each unit has several children and each children has several contents. Each content id matches just one children id and each children id matches just one unit id. Ids have to be selected on a random basis.
I've tried to extract all ids from the response and use them randomly, but it doesn't work this way.
To extract only Unit Ids, you can use following JSON Path Expressions:
$..data.attributes.units[?(#.id)].id
Random Value for Unit Id also can be extracted using JMeter JSON Extractor:

JQ: Set toplevel path

I use jq 1.5 under a Windows enviroment to modify a given large json file to extract a single Array ("Offers") from that large file:
'.Offers[] | ({Price: .AdultPriceEUR, Currency: .Currency, Link: .Deeplink, Tickettyp: .TicketClassIndex, Flightindex: .FlightIndex })'
After that i got an "unnamed" Array. But for the later processing it is necessary that the Array keeps his old "Name". I checked the documentation and found the setpath function but it is not possible to keep the Name "easy" on extraction?
shorten example of the json file:
{"Airports": [
{
"Aliases": null,
"ContinentCode": "EU",
"ContinentGroup": 1,
"CountryCode": "DE",
"CountryName": "Germany",
"DST": "",
"DisplayName": "Hamburg (HAM) Germany",
"Iata": "HAM",
"IataLink": false,
"Icao": "EDDH",
"Latitude": 53.63215,
"Longitude": 10.0041609,
"MainCityCode": "HAM",
"MainCityDisplayName": "Hamburg (HAM) Germany",
"MainCityName": "Hamburg",
"Name": "Hamburg",
"Priority": 142,
"StateCode": null,
"StateName": null,
"TimeZone": -798214753
},
{
"Aliases": null,
"ContinentCode": "AS",
"ContinentGroup": 4,
"CountryCode": "TH",
"CountryName": "Thailand",
"DST": "",
"DisplayName": "Suvarnabhumi, Bangkok (BKK) Thailand",
"Iata": "BKK",
"IataLink": false,
"Icao": "VTBS",
"Latitude": 13.6922979,
"Longitude": 100.750694,
"MainCityCode": "BKK",
"MainCityDisplayName": "Bangkok (BKK) Thailand",
"MainCityName": "Bangkok",
"Name": "Suvarnabhumi",
"Priority": 1462,
"StateCode": null,
"StateName": null,
"TimeZone": -640089798
}], "Offers": [
{
"AdultPrice": 2977.6,
"AdultPriceEUR": 2977.6,
"AdultPriceExclTax": 0.0,
"Currency": "EUR",
"FeeIndexes": [
0,
1,
2,
3,
4,
5,
6
],
"FlightIndex": 0,
"IsPaymentIncluded": true,
"MobileDeepLink": null,
"PaymentMethods": [
"American Express",
"Diners Club",
"MasterCard Credit",
"MasterCard Debit",
"Paypal",
"Visa Credit",
"Visa Debit"
],
"Score": 2501.3,
"SegmentFares": null,
"SegmentKey": -1,
"TicketClassIndex": 1,
"TotalIsCalculated": false,
"TotalPrice": 2977.6,
"TotalPriceEUR": 2977.6,
"TotalPriceExclTax": 0.0
},
{
"AdultPrice": 4697.27,
"AdultPriceEUR": 4697.27,
"AdultPriceExclTax": 0.0,
"Currency": "EUR",
"FeeIndexes": [
0,
1,
2,
3,
4,
7,
8,
5,
6
],
"FlightIndex": 1,
"IsPaymentIncluded": true,
"MobileDeepLink": null,
"PaymentMethods": [
"American Express",
"Diners Club",
"MasterCard Credit",
"MasterCard Debit",
"Paypal",
"Sofortüberweisung",
"Überweisung",
"Visa Credit",
"Visa Debit"
],
"Score": 3438.64,
"SegmentFares": null,
"SegmentKey": -1,
"TicketClassIndex": 1,
"TotalIsCalculated": false,
"TotalPrice": 4697.27,
"TotalPriceEUR": 4697.27,
"TotalPriceExclTax": 0.0
}]
}
thanks
BR
Timo
Looks like you're looking at this:
jq '{Offers:[.Offers[] | {Price: .AdultPriceEUR, Currency: .Currency, Link: .Deeplink, Tickettyp: .TicketClassIndex, Flightindex: .FlightIndex }]}' file
It just creates a new object containing an Offers table with the content you want to put it it.