change the order of json values - json

I have a json data as follows
[{
"_id": "52cbafc5afcec877d925d116",
"id": "52",
"url": "172.34.56.7"
}, {
"_id": "52c255e5cf0060b8cce84b4a",
"id": "13",
"url": "172.18.2.114"
}, {
"_id": "52cbd018afcec877d925d131",
"id": "49",
"url": "172.16.2.120"
}, {
"_id": "52d60c9eafcec877d925d17a",
"id": "100",
"url": "156.45.34.56"
}]
I want to print the data as below.(last data comes first)
[{
"_id": "52d60c9eafcec877d925d17a",
"id": "100",
"url": "156.45.34.56"
}, {
"_id": "52cbd018afcec877d925d131",
"id": "49",
"url": "172.16.2.120"
}, {
"_id": "52c255e5cf0060b8cce84b4a",
"id": "13",
"url": "172.18.2.114"
},
[{
"_id": "52cbafc5afcec877d925d116",
"id": "52",
"url": "172.34.56.7"
}]
How it is possible

Implement a Comparator and then sort your Json array with it.

This can be done using
data=data.reverse()

Related

JSON Parsing a Plain Text File

I have a lot of data I need to parse though.
I need to pull all pid's and price's.
`
[
{
"id": 159817,
"price": "10.69",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 24549,
"pid": "12079",
"labels": [
{
"type": "default",
"value": "Chicken Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Chicken",
"locale": "en"
},
{
"type": "product_json",
"value": "Chicken",
"locale": "en"
}
],
"calMin": 600,
"calMax": 600,
"lastUpdated": "2021-12-31T13:49:22.794Z"
}
},
{
"id": 159818,
"price": "9.29",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 25,
"pid": "1",
"labels": [
{
"type": "default",
"value": "Ham Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Ham",
"locale": "en"
}
],
"calMin": 540,
"calMax": 540,
"lastUpdated": "2021-07-09T19:30:00.326Z"
}
}
]
`
and I need to place them into a string like this, but on a scale of 150 products. I'd also need to change "pid" to "productId"
[{ "productId": "46238", "price": 6.09 }, { "productId": "40240", "price": 1.49 }]
I need to add a string before this data, but I'm pretty confident I can figure that part out.
I am pretty open to the easiest suggestion, whether that be VBS, Excel macro, etc.

Moving a JSON array which have another array elements to the same LEVEL

I have the following JSON which have an element array_within_array in another array. So I want to pull both array_within_array element and upper array element(event_array) to root level using jq.
So here there are 3 events.
meal_selection
login
placed_order
Event placed_order have two sub-events in array. So after conversion there should be 4 events(1 from meal_selection, 1 from login and 2 from placed_order). these all should be on the same level.
Here is the JSON
{
"region": "USA",
"user_id": "123",
"event_array": [{
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
},
{
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
},
{
"event_attributes": {
"array_within_array": [
{
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
{
"date": "2021-08-18",
"category": "dinner",
"location": "home"
}
]
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
]
}
and I want to convert to the below one
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
}
{
"region": "USA",
"user_id": "123",
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-18",
"category": "dinner",
"location": "home"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
Here's a straightforward solution that keeps things simple by using two steps:
{ region, user_id} + (.event_array[] )
| if .event_attributes|has("array_within_array")
then .event_attributes.array_within_array as $a
| .event_attributes = $a[]
else .
end

Remove values of a string in a JSON file

I have a JSON file containing the following:
[
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
}
]
I would like to remove the values after date, number and ID. I would like to do this using a powershell script.
My expected outcome is:
[
{
"Date": "",
"Number": "",
"ID": ""
},
{
"Date": "",
"Number": "",
"ID": ""
},
{
"Date": "",
"Number": "",
"ID": ""
},
}
]
Can someone help me to achieve this? Kind Regards!
I would suggest iterating over the array and just set the properties to an empty string:
#'
[
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
{
"Date": "05082021",
"Number": "123456789",
"ID": "123456789"
},
]
'# | ConvertFrom-Json | ForEach-Object {
$_.Date = [string]::Empty
$_.Number = [string]::Empty
$_.ID = [string]::Empty
$_
} | ConvertTo-Json

How to confirm Webhook POST is being received in Coldfusion

To be clear upfront, I'm not a professional - just trying to get a task done and I have minimal expertise. What I thought would be easy is apparently not so. I've dealt with APIs and XML but it's always been me initiating. This is apparently different.
Goal:
Receive HTTP POST Webhook data (JSON) from my Wordpress/Woocommerce website.
Problem:
The Webhook is working and being received perfectly by RequestBin.The JSON string is being sent over HTTP. But I'm not even sure the request is being heard by my CF Template. When I try to write the received data into a session variable so I can at least see it, nothing is there?!?! How do I find out if my code being triggered?
First line of code in my CF Template:
<cfset HTTPRequestData = deserializeJSON(ToString(getHTTPRequestData().content))>
Example from RequestBin of data being sent:
{
"id": 259609,
"parent_id": 0,
"number": "CW-104-270219",
"order_key": "xxxxxxxxxxxx",
"created_via": "checkout",
"version": "3.5.5",
"status": "completed",
"currency": "USD",
"date_created": "2019-02-27T15:11:58",
"date_created_gmt": "2019-02-27T20:11:58",
"date_modified": "2019-02-27T15:11:59",
"date_modified_gmt": "2019-02-27T20:11:59",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "9.68",
"total_tax": "0.00",
"prices_include_tax": false,
"customer_id": 2,
"customer_ip_address": "xxxxxxxxxxxx",
"customer_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36",
"customer_note": "",
"billing": {
"first_name": "Valerie",
"last_name": "Criswell",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "xxxxxxxxxx",
"phone": ""
},
"shipping": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "authorize_net_aim",
"payment_method_title": "Credit Card",
"transaction_id": "xxxxxxxxxxxxx",
"date_paid": "2019-02-27T15:11:59",
"date_paid_gmt": "2019-02-27T20:11:59",
"date_completed": "2019-02-27T15:11:59",
"date_completed_gmt": "2019-02-27T20:11:59",
"cart_hash": "xxxxxxxxxxxxx",
"meta_data": [
{
"id": 8785641,
"key": "_wcson_order_number",
"value": "CWD-259609-022719-108"
},
{
"id": 8785642,
"key": "_order_number",
"value": "104"
},
{
"id": 8785643,
"key": "_order_number_formatted",
"value": "CW-104-270219"
},
{
"id": 8785644,
"key": "_order_number_meta",
"value": {
"prefix": "CW-",
"suffix": "-{DD}{MM}{YY}",
"length": "2"
}
},
{
"id": 8785645,
"key": "_wc_authorize_net_aim_retry_count",
"value": "0"
},
{
"id": 8785646,
"key": "_wc_authorize_net_aim_trans_id",
"value": "xxxxxxxx"
},
{
"id": 8785647,
"key": "_wc_authorize_net_aim_trans_date",
"value": "2019-02-27 15:11:59"
},
{
"id": 8785648,
"key": "_wc_authorize_net_aim_environment",
"value": "production"
},
{
"id": 8785649,
"key": "_wc_authorize_net_aim_account_four",
"value": "xxxxxxxx"
},
{
"id": 8785650,
"key": "_wc_authorize_net_aim_authorization_amount",
"value": "xxxxxxx"
},
{
"id": 8785651,
"key": "_wc_authorize_net_aim_authorization_code",
"value": "xxxxxxx"
},
{
"id": 8785652,
"key": "_wc_authorize_net_aim_charge_captured",
"value": "yes"
},
{
"id": 8785653,
"key": "_wc_authorize_net_aim_card_expiry_date",
"value": "21-08"
},
{
"id": 8785654,
"key": "_wc_authorize_net_aim_card_type",
"value": "mastercard"
},
{
"id": 8785659,
"key": "_pip_invoice_number",
"value": "CW-104-270219"
},
{
"id": 8785660,
"key": "_wc_pip_invoice_email_count",
"value": "1"
},
{
"id": 8785661,
"key": "_wc_pip_packing_list_email_count",
"value": "1"
}
],
"line_items": [
{
"id": 17,
"name": "I Give Myself Away - MP3 Download - 30 Day Access",
"product_id": 192127,
"variation_id": 192128,
"quantity": 1,
"tax_class": "",
"subtotal": "9.68",
"subtotal_tax": "0.00",
"total": "9.68",
"total_tax": "0.00",
"taxes": [],
"meta_data": [
{
"id": 182,
"key": "pa_format",
"value": "download-30-day"
}
],
"sku": "131599-DL30DAY",
"price": 9.68
}
],
"tax_lines": [],
"shipping_lines": [],
"fee_lines": [],
"coupon_lines": [],
"refunds": []
}
Let's start with the stated goal:
Receive HTTP POST Webhook data from my Wordpress/Woocommerce website.
Can your Wordpress site hit the intended URL on your ColdFusion site? If so, can you record / dump the HTTP POST request to a file?
<cfdump var="#form#" format="html" output="/full/path/to/file">
This will tell you if you're even receiving the request correctly.

How to add label to vertices in GRAPHSON (a JSON based format for graph elements)

With reference To link
https://github.com/tinkerpop/blueprints/wiki/GraphSON-Reader-and-Writer-Library
How to label the vertices in the json file which will result in speeding up graph traversals. I am using titan graph db . So GRAPHSON will be used for converting json into graph instance and also using gremlin query language . And thus to speed up the retrieval of vertices i need to label these vertices to categorise . How Can i add label ??
{
"mode":"EXTENDED",
"vertices": [
{
"name": {
"type": "string",
"value": "lop"
},
"lang": {
"type": "string",
"value": "java"
},
"_id": "3",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "vadas"
},
"age": {
"type": "integer",
"value": 27
},
"_id": "2",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "marko"
},
"age": {
"type": "integer",
"value": 29
},
"_id": "1",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "peter"
},
"age": {
"type": "integer",
"value": 35
},
"_id": "6",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "ripple"
},
"lang": {
"type": "string",
"value": "java"
},
"_id": "5",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "josh"
},
"age": {
"type": "integer",
"value": 32
},
"_id": "4",
"_type": "vertex"
}
],
"edges": [
{
"weight": {
"type": "float",
"value": 1
},
"_id": "10",
"_type": "edge",
"_outV": "4",
"_inV": "5",
"_label": "created"
},
{
"weight": {
"type": "float",
"value": 0.5
},
"_id": "7",
"_type": "edge",
"_outV": "1",
"_inV": "2",
"_label": "knows"
},
{
"weight": {
"type": "float",
"value": 0.4000000059604645
},
"_id": "9",
"_type": "edge",
"_outV": "1",
"_inV": "3",
"_label": "created"
},
{
"weight": {
"type": "float",
"value": 1
},
"_id": "8",
"_type": "edge",
"_outV": "1",
"_inV": "4",
"_label": "knows"
},
{
"weight": {
"type": "float",
"value": 0.4000000059604645
},
"_id": "11",
"_type": "edge",
"_outV": "4",
"_inV": "3",
"_label": "created"
},
{
"weight": {
"type": "float",
"value": 0.20000000298023224
},
"_id": "12",
"_type": "edge",
"_outV": "6",
"_inV": "3",
"_label": "created"
}
]
}
TinkerPop 2.x did not have native support for vertex labels and thus, GraphSON 2.x didn't support it either. TinkerPop 3.x (and thus Titan 1.0) have this support:
http://tinkerpop.incubator.apache.org/docs/3.0.1-incubating/#graphson-reader-writer
You'd either need to upgrade to Titan 1.0/TP3 or write a custom GraphSON processor using the Titan API in earlier versions of Titan/TP2.