How to extract specific object from JSON with arrays via jq - arrays

How can I extract only the objects id and name from this JSON via jq?
The output should be look like the format below. This is just an example for the required output, the real one that I need is to catch the whole values id and name like in the JSON source.
This is the required output:
{
"name": "Auto Body Styles",
"id": "1.1"}
{
"name": "Convertible",
"id": "1.1.2"
}
This is the JSON source file:
{
"name": "Automotive",
"id": "1",
"categories": [
{
"name": "Auto Body Styles",
"id": "1.1",
"categories": [
{
"name": "Commercial Trucks",
"id": "1.1.1"
},
{
"name": "Convertible",
"id": "1.1.2"
},
{
"name": "Coupe",
"id": "1.1.3"
},
{
"name": "Crossover",
"id": "1.1.4"
},
{
"name": "Hatchback",
"id": "1.1.5"
},
{
"name": "Microcar",
"id": "1.1.6"
},
{
"name": "Minivan",
"id": "1.1.7"
},
{
"name": "Off-Road Vehicles",
"id": "1.1.8"
},
{
"name": "Pickup Trucks",
"id": "1.1.9"
},
{
"name": "Sedan",
"id": "1.1.10"
},
{
"name": "Station Wagon",
"id": "1.1.11"
},
{
"name": "SUV",
"id": "1.1.12"
},
{
"name": "Van",
"id": "1.1.13"
}
]
},
{
"name": "Auto Buying and Selling",
"id": "1.2"
},
{
"name": "Auto Insurance",
"id": "1.3"
},
{
"name": "Auto Parts",
"id": "1.4"
},
{
"name": "Auto Recalls",
"id": "1.5"
},
{
"name": "Auto Repair",
"id": "1.6"
},
{
"name": "Auto Safety",
"id": "1.7"
},
{
"name": "Auto Shows",
"id": "1.8"
},
{
"name": "Auto Technology",
"id": "1.9",
"categories": [
{
"name": "Auto Infotainment Technologies",
"id": "1.9.1"
},
{
"name": "Auto Navigation Systems",
"id": "1.9.2"
},
{
"name": "Auto Safety Technologies",
"id": "1.9.3"
}
]
},
{
"name": "Auto Type",
"id": "1.10",
"categories": [
{
"name": "Budget Cars",
"id": "1.10.1"
},
{
"name": "Certified Pre-Owned Cars",
"id": "1.10.2"
},
{
"name": "Classic Cars",
"id": "1.10.3"
},
{
"name": "Concept Cars",
"id": "1.10.4"
},
{
"name": "Driverless Cars",
"id": "1.10.5"
},
{
"name": "Green Vehicles",
"id": "1.10.6"
}
]
} ] }

i think what you want is Recursive Descent: ..
cat car.json | jq -r '.. | [.name?, .id?] | select(length>0) | #tsv'
to produce something like in your example,
cat car.json | jq -r '.. | {name:.name?, id:.id?}'

Related

Parse this Json array and extract the value of text

Here is the full json that I want to parse and extract the text that has the value: The product is in second line 3rd row.
Can someone help?
"activities": [
{
"type": "message",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"timestamp": "2019-07-01T15:18:56.8251462Z",
"serviceUrl": "XXXXXXXXXXXXXXXXXXXXXXXXX",
"channelId": "directline",
"from": {
"id": "user1"
},
"conversation": {
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"text": "the milk"
},
{
"type": "message",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"timestamp": "2019-07-01T15:18:57.6856172Z",
"localTimestamp": "2019-07-01T15:18:57.5099359+00:00",
"channelId": "directline",
"from": {
"id": "XXXXXX",
"name": "XXXXXX"
},
"conversation": {
"id": "XXXXXXXXXXXXXXXXXX"
},
"text": "The product is in second line 3rd row",
"attachments": [],
"entities": [],
"replyToId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
],
"watermark": "1"
}
You have specified that this is a full json you have placed in the question, But it is not full json you are missing something.
"activities":
[ //json array start from here
{
"type": "message",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"timestamp": "2019-07-01T15:18:56.8251462Z",
"serviceUrl": "XXXXXXXXXXXXXXXXXXXXXXXXX",
"channelId": "directline",
"from": {
"id": "user1"
},
"conversation": {
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"text": "the milk"
},
{
"type": "message",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"timestamp": "2019-07-01T15:18:57.6856172Z",
"localTimestamp": "2019-07-01T15:18:57.5099359+00:00",
"channelId": "directline",
"from": {
"id": "XXXXXX",
"name": "XXXXXX"
},
"conversation": {
"id": "XXXXXXXXXXXXXXXXXX"
},
"text": "The product is in second line 3rd row",
"attachments": [],
"entities": [],
"replyToId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
], //json array ends here
these are extra lines
***"watermark": "1"
}***
if you want to use this array you need to add a '{' in very top line before
"activities": [
I am placing here a valid json now you can check it on Json parser used to parse json in objects
*Valid Json Here : *
{
"activities": [
{
"type": "message",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"timestamp": "2019-07-01T15:18:56.8251462Z",
"serviceUrl": "XXXXXXXXXXXXXXXXXXXXXXXXX",
"channelId": "directline",
"from": {
"id": "user1"
},
"conversation": {
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"text": "the milk"
},
{
"type": "message",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"timestamp": "2019-07-01T15:18:57.6856172Z",
"localTimestamp": "2019-07-01T15:18:57.5099359+00:00",
"channelId": "directline",
"from": {
"id": "XXXXXX",
"name": "XXXXXX"
},
"conversation": {
"id": "XXXXXXXXXXXXXXXXXX"
},
"text": "The product is in second line 3rd row",
"attachments": [],
"entities": [],
"replyToId": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
],
"watermark": "1"
}

Json schema for array of objects doesn't validate

I have this schema for a json response
{
"title": "Products",
"description": "schema for products",
"type": "array",
"properties": {
"id": {
"description": "id of a product",
"type": "integer"
},
"name": {
"description": "name of the product",
"type": "string"
},
"created_at": {
"description": "record created_at",
"type": "string",
"format": "date-time"
},
"updated_at": {
"description": "record updated_at",
"type": "string",
"format": "date-time"
}
},
"required": ["id", "name"]
}
and I want to match this schema with this json
[{
"id": 1,
"name": "Cricket Ball"
}, {
"id": 2,
"name": "Soccer Ball"
}, {
"id": 3,
"name": "football ball"
}, {
"id": 4,
"name": "Basketball ball"
}, {
"id": 5,
"name": "Table Tennis ball"
}, {
"id": 6,
"name": "Tennis ball"
}]
This schema matches the response but it also matches the schema in which the required field is this
"required": ["ids", "names"]
I think the schema is validated against the array and the objects in the array are not validated.
The way you have it set up now, your properties key refers to the array itself, not to each item, and is being ignored (because arrays don't have properties, they just have items). You need to use the items key to validate each item in the array, like so:
{
"title": "Products",
"description": "schema for products",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"description": "id of a product",
"type": "integer"
},
"name": {
"description": "name of the product",
"type": "string"
},
"created_at": {
"description": "record created_at",
"type": "string",
"format": "date-time"
},
"updated_at": {
"description": "record updated_at",
"type": "string",
"format": "date-time"
}
},
"required": ["id", "name"]
}
}
try map
new_array = response.map{ |k| { 'id': k['properties']['id']['description'], 'name': k['properties']['name']['description'] } }

Filtering JSON data in a list

I am new to working with JSON data. I am having difficulty on how to return back bicycles that have a color of "Blue". Using JSONPath.
The following example JSON file.
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
"bicycle": [
{
"price": 19.95
"color": [
"red"
],
},
{
"price": 20.99
"color": [
"blue",
"Green"
],
},
]
}
}
I have tried to use the following filter but it doesn't work.
$.store.bicycle[?(#.color=='blue')]
Any ideas to how to get this to work and only return the price of bicycles that are Blue?
Any information is greatly welcomed.
The JSON Data is
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}],
"bicycle": [
{
"price": 19.95,
"color": [
"red"
]
},
{
"price": 20.99,
"color": [
"blue",
"Green"
]
}
]
}
}
The JSONPath is
$.store.bicycle[?(#.color.indexOf('blue') != -1)]

Sugar CRM 6.5 API

I am new to the Sugar CRM 6.5 API. I am having trouble getting my url to post data to the leads module in Sugar. Here is the posting url,
https://yoursite/service/v4_1/rest.php?method=set_entry&input_type=JSON&response_type=JSON&rest_data={"name_value_list":{"last_name":"Peter","status":"New","email1":"test#test.com","lead_source":"test"},"module_name":"Leads"}
Here is my response,
{
"id": "459bf4a2-0b3b-5857-feb5-56fe81e621eb",
"entry_list": {
"modified_by_name": {
"name": "modified_by_name",
"value": "admin"
},
"id": {
"name": "id",
"value": "459bf4a2-0b3b-5857-feb5-56fe81e621eb"
},
"name": {
"name": "name",
"value": " "
},
"date_entered": {
"name": "date_entered",
"value": "2016-04-01 14:10:12"
},
"date_modified": {
"name": "date_modified",
"value": "2016-04-01 14:10:12"
},
"modified_user_id": {
"name": "modified_user_id",
"value": "1"
},
"created_by": {
"name": "created_by",
"value": "1"
},
"deleted": {
"name": "deleted",
"value": 0
},
"full_name": {
"name": "full_name",
"value": " "
},
"do_not_call": {
"name": "do_not_call",
"value": false
},
"converted": {
"name": "converted",
"value": false
},
"lead_source": {
"name": "lead_source",
"value": "Web Site"
},
"status": {
"name": "status",
"value": "New"
},
"jjwg_maps_lat_c": {
"name": "jjwg_maps_lat_c",
"value": 0
},
"jjwg_maps_lng_c": {
"name": "jjwg_maps_lng_c",
"value": 0
}
}
}
When I view the leads, I get a blank record created. Any idea's, Thanks
The format of name_value_list is as follow:
{
"name_value_list":
"full_name": {"name": "full_name", "value": "John"}
}

Multi-dimensional arrays in Mandrill with handlebars

I'm trying to loop through a multi-dimensional array to get properties of products that are part of line items. They look basically like this: (I did a json_encode so it would be easier to read)
[{
"rcpt": "email#email.com",
"vars": [{
"name": "SYSTEM",
"content": "Bikes"
}, {
"name": "CUSTOMERSERVICE",
"content": "(855-553-4889)"
}, {
"name": "IMAGE",
"content": "http:\/\/www.url.com\/assets\/images\/chicago\/email\/dear_member.jpg"
}, {
"name": "LINKCOLOR",
"content": "#3db7e4"
}, {
"name": "FACEBOOK",
"content": "Bikes"
}, {
"name": "TWITTER",
"content": "Bikes"
}, {
"name": "INSTAGRAM",
"content": "Bikes"
}, {
"name": "CLOSING",
"content": "Greetings"
}, {
"name": "item",
"content": [{
"lineItem": 1,
"id": "3",
"name": "24-Hour Pass Gift Certificate",
"quantity": 2,
"nameShort": "24-Hour",
"type": "Gift Certificate",
"image": "24hour_blank.jpg",
"price": "9.95",
"total": "19.90",
"taxable": false,
"giftCertificates": {
"3204": {
"id": "3204",
"redemptionNumber": "xxxxx",
"type": "24-Hour"
},
"3205": {
"id": "3205",
"redemptionNumber": "xxxxx",
"type": "24-Hour"
}
}
}, {
"lineItem": 2,
"id": "1",
"name": "Annual Membership Gift Certificate",
"quantity": 2,
"nameShort": "Annual",
"type": "Gift Certificate",
"image": "annual_blank.jpg",
"price": "75.00",
"total": "150.00",
"taxable": false,
"giftCertificates": {
"892": {
"id": "892",
"redemptionNumber": "xxxxxx",
"type": "Annual"
},
"893": {
"id": "893",
"redemptionNumber": "xxxxx",
"type": "Annual"
}
}
}]
}, {
"name": "orderID",
"content": 1220
}, {
"name": "giftMessage",
"content": false
}, {
"name": "email",
"content": "email#email.com"
}, {
"name": "transactionDate",
"content": "12\/23\/2015"
}, {
"name": "transactionTime",
"content": "12:21 pm"
}, {
"name": "salesTaxTotal",
"content": 0
}, {
"name": "salesTaxRatePercent",
"content": "6.250"
}, {
"name": "TransactionAmount",
"content": "169.90"
}, {
"name": "account_number",
"content": "XXXX1111"
}, {
"name": "card_type",
"content": "Visa"
}, {
"name": "firstName",
"content": "tetete"
}, {
"name": "lastName",
"content": "tethuhhu"
}, {
"name": "address",
"content": "295 Place St"
}, {
"name": "city",
"content": "Brooklyn"
}, {
"name": "state",
"content": "NY"
}, {
"name": "zip",
"content": "11238"
}, {
"name": "country",
"content": "US"
}, {
"name": "phone",
"content": "8888888888"
}, {
"name": "transactionId",
"content": "xxxxxx"
}, {
"name": "shipToFirstName",
"content": "tetete"
}, {
"name": "shipToLastName",
"content": "tethuhhu"
}, {
"name": "shipToAaddress",
"content": "295 Place St"
}, {
"name": "shipToCity",
"content": "Brooklyn"
}, {
"name": "shipToState",
"content": "NY"
}, {
"name": "shipToZipCode",
"content": "11238"
}, {
"name": "ShipToCountry",
"content": "US"
}, {
"name": "ShipToCountry",
"content": "US"
}]
}]
So I am trying to get a print out of each gift certificate's type and redemption number. When I iterate through {{ giftCertificates }} like this:
{{#each giftCertificates}}
{{type}} {{redemptionNumber}}
{{/each}}
I get one of the line items but not the other. I'm guessing maybe it is being overwritten when it loops through again? But I have also tried to loop through {{ item }} and grab {{ giftCertificates.type }} and {{ giftCertificates.redemptionNumber }} and that does not work either. What is the correct way to get all of these from each line item?
Thanks for your help.
I know this is a very old question, but:
you can use {{this.proprietyName}} to get the type and number:
{{#each giftCertificates}}
{{this.892.type}}
{{/each}}
do not forget to add this to the mandrill message o
"merge": true,
"merge_language": "handlebars",
Also, the data structure is not ideal:
giftCertificates[
{
"id": "892",
"redemptionNumber": "xxxxxx",
"type": "Annual"
},
{
"id": "893",
"redemptionNumber": "xxxxxx",
"type": "Annual"
}
]
would be easier to handle.

Resources