I have a json file say file.json
It has a json body like this
{
"input": {
"url": "/abc/def/efg/{UUID}.txt",
"type": "text"
},
"output": {
"result": 10,
"content": ""
}
}
{UUID} is any valid UUID.
How do I allow any UUID as regex / generic form in place {UUID} ??
You can use this regex to find and match the 'GUID' part or your text:
/\"url\":\s?\"[^{]+\{([^}]+)/
The regex matches '"url": "' at start, then matches up to '{' before it creates a Group where it stores the 'UUID'
You get an array as result, where you should grap Group 1.
How to use:
if your text is in a variable called 'text', then you can do:
var UUID = text.match(/\"url\":\s?\"[^{]+\{([^}]+)/)[1];
Related
I have JSON that contains a value that is a string of pipe-separated key-value pairs that I need to pull a value from using JSONPath expression with a script.
I'm looking to return all the "Job Titles" found in the third position of the "Rows" object using JSONPath.
Here is the original JSON
{
"DataSource": {
"Rows": [
[
"Leslie Knope",
"Eastern Standard Time",
"USA",
"Department:Parks and Recreation|Job Title:Project Manager|Email:leslie#pawnee.il.gov"
],
[
"Ron Swanson",
"Eastern Standard Time",
"USA",
"Department:Parks and Recreation|Job Title:Senior Project Manager|Email:ron#pawnee.il.gov"
]
]
}
}
To select the details I need, I would use the JSONPath Expression "$.DataSource.Rows[*].3" and it would return:
[
"Department:Parks and Recreation|Job Title:Project Manager|Email:leslie#pawnee.il.gov",
"Department:Parks and Recreation|Job Title:Senior Project Manager|Email:ron#pawnee.il.gov"
]
I know that I can use regex to extract the value for "Job Title" and that Regex is "/(?<=Job Title:)(.*?)(?=[|]|$)/gm".
Is there a way to use JSON path (along with Regex) to return only the values for 'Job Title' from the JSON?
The output I'm looking for is:
[
"Project Manager",
"Senior Project Manager"
]
Is this possible to do this using JSONPath with a script? I'm stumped!
you can use a map function instead
var result = json.DataSource.Rows.map((element) => {
return element[3].substring(
element[3].indexOf("Title:") + 6,
element[3].indexOf("Email:") - 1
);
});
or if you want json path
var result = jsonPath(json, "$.DataSource.Rows[*].3").map((element) => {
return element.substring(
element.indexOf("Title:") + 6,
element.indexOf("Email") - 1
);
});
I am trying to match some parameters with json reposne
my actual response is like
{
"timestamp": 1595994767386,
"country": "MH",
"accessible_device_types": [
{
"name": "ESS Client",
"raw_name": "ABC",
"permission": 7,
"permission_bits": {
"INSTALL_LIMITED_RELEASE_SOFTWARE": true,
"INSTALL_LATEST_SOFTWARE_ONLY": true,
"INSTALL_SOFTWARE": true
}
},
used below statment for comparing:
match response.accessible_device_types contains [{"raw_name": "ABC"}]
Reason for error from report: expected: {raw_name=ABC}, reason: actual value does not contain expected
Looks like comparing without quotes. Why is it taking out the quotes? Any recommendations
How to compare "INSTALL_SOFTWARE": true
2 options:
* def nameAbc = {"raw_name": "ABC"}
* match response.accessible_device_types contains '#(^nameAbc)'
This will work in 0.9.6.RC4 onwards:
* match response.accessible_device_types contains deep {"raw_name": "ABC"}
I have following flow in NIFI , JSON has (1000+) objects in it.
invokeHTTP->SPLIT JSON->putMongo
Flow works fine, till I receive some keys in json with "." in the name. e.g. "spark.databricks.acl.dfAclsEnabled".
my current solution is not optimal, I have jotted down bad keys, and using multiple replace text processor to replace "." with "_". I am not using REGEX, I am using string literal find/replace. So each time I am getting failure in putMongo processor, I am inserting new replaceText processor.
This is not maintainable. I am wondering if I can use JOLT for this? couple of info regarding input JSON.
1) no set structure, only thing that is confirmed is. everything will be in events array. But event object itself is free form.
2) maximum list size = 1000.
3) 3rd party JSON, so I cant ask for change in format.
Also, key with ".", can appear anywhere. So I am looking for JOLT spec that can cleanse at all level and then rename it.
{
"events": [
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1531896847915,
"type": "EDITED",
"details": {
"previous_attributes": {
"cluster_name": "Kylo",
"spark_version": "4.1.x-scala2.11",
"spark_conf": {
"spark.databricks.acl.dfAclsEnabled": "true",
"spark.databricks.repl.allowedLanguages": "python,sql"
},
"node_type_id": "Standard_DS3_v2",
"driver_node_type_id": "Standard_DS3_v2",
"autotermination_minutes": 10,
"enable_elastic_disk": true,
"cluster_source": "UI"
},
"attributes": {
"cluster_name": "Kylo",
"spark_version": "4.1.x-scala2.11",
"node_type_id": "Standard_DS3_v2",
"driver_node_type_id": "Standard_DS3_v2",
"autotermination_minutes": 10,
"enable_elastic_disk": true,
"cluster_source": "UI"
},
"previous_cluster_size": {
"autoscale": {
"min_workers": 1,
"max_workers": 8
}
},
"cluster_size": {
"autoscale": {
"min_workers": 1,
"max_workers": 8
}
},
"user": ""
}
},
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1535540053785,
"type": "TERMINATING",
"details": {
"reason": {
"code": "INACTIVITY",
"parameters": {
"inactivity_duration_min": "15"
}
}
}
},
{
"cluster_id": "0717-035521-puny598",
"timestamp": 1535537117300,
"type": "EXPANDED_DISK",
"details": {
"previous_disk_size": 29454626816,
"disk_size": 136828809216,
"free_space": 17151311872,
"instance_id": "6cea5c332af94d7f85aff23e5d8cea37"
}
}
]
}
I created a template using ReplaceText and RouteOnContent to perform this task. The loop is required because the regex only replaces the first . in the JSON key on each pass. You might be able to refine this to perform all substitutions in a single pass, but after fuzzing the regex with the look-ahead and look-behind groups for a few minutes, re-routing was faster. I verified this works with the JSON you provided, and also JSON with the keys and values on different lines (: on either):
...
"spark_conf": {
"spark.databricks.acl.dfAclsEnabled":
"true",
"spark.databricks.repl.allowedLanguages"
: "python,sql"
},
...
You could also use an ExecuteScript processor with Groovy to ingest the JSON, quickly filter all JSON keys that contain ., perform a collect operation to do the replacement, and re-insert the keys in the JSON data if you want a single processor to do this in a single pass.
I am currently using the Cassandra-Ruby driver to insert data from a JSON file into an existing table in my database.
the JSON file looks like this:
[
{
"id": "123",
"destination": "234",
"type": "equipment",
"support": "type 1",
"test": "test1"
},
{
"id": "234",
"destination": "123",
"type": "equipment",
"support": "type 1",
"test": "test1"
}
]
I am reading in the file like this:
file = File.read('itemType.json')
data_hash = JSON.parse(file) #return an array of hashes
Iterate through the array and get each hash
and insert each hash onto the table
data_hash.each do |has|
#check the type of each object
#puts has.class #return hash
insert_statement = session.prepare('INSERT INTO keyspace.table JSON ?')
session.execute(insert_statement, [has]) #error occurs here
end
After running this code, I get this error message
in `assert_instance_of': options must be a Hash
I checked that each object being inserted in the table is a hash, so I'm not sure why I'm getting this issue.
You are saying that you are inserting a JSON but you are not, you are trying to insert an object. See this example from the documentation:
INSERT INTO cycling.cyclist_category JSON '{
"category" : "Sprint",
"points" : 700,
"id" : "829aa84a-4bba-411f-a4fb-38167a987cda"
}';
You have to give it a json format if you do it like that.
using .to_json add \ escape character. This gave me error
INSERT INTO organization_metadata JSON '{\"id\":9150,\"destroyed\":false,\"name\":\"ABC\",\"timestamp\":1510541801000000000}';
and the following worked.
INSERT INTO organization_metadata JSON '{"id":9150,"destroyed":false,"name":"ABC","timestamp":1510541801000000000}';
I have a table where the data field has JSONB type and among many other data I have a notes key inside the data json value where I store an array of notes.
Each note has (at least) two fields: title and content.
Sometimes I have to replace the whole list of notes with a different list, but not affecting any other fields inside my json record.
I tried something like this:
UPDATE mytable
SET data = jsonb_set("data", '{notes}', '[{ "title": "foo1" "content": "bar"'}, { "title": "foo2" "content": "bar2"}]', true)
WHERE id = ?
And I get an exception (through a js wrapper)
error: invalid input syntax for type json
How should I correctly use the jsonb_set function?
You have a stray single quote and missing commas in your JSON payload
Instead of
[{ "title": "foo1" "content": "bar"'}, { "title": "foo2" "content": "bar2"}]
^ ^ ^
it should rather look
[{ "title": "foo1", "content": "bar"}, { "title": "foo2", "content": "bar2"}]