How to mass-replace text in "sounds" : ["test/test"] - json

I'm trying to make a resource pack in Minecraft, and I'm replacing it so there's only one sound. When I went to go and edit sounds.json in VSC, I want to set all the locations to just one file. It should look like this :
"sounds" : [
"test/test"
],
to test/test for all the "sounds". But I have no idea of how to do this. The sounds.json file is so big it would take more than a day to do all the work by hand. So I checked to see if VSC had any options to replace the text. There wasn't.
I've tried looking around in VSC and there wasn't anything useful.
I've tried replacing all the sounds by pasting .ogg files and renaming them, it took too long, so I realized I could just set all the locations to point at one sound file.
I've gone on Google to do some research but found nothing of use.
"block.enderchest.open": {
"sounds": [
"test/test"
],
"subtitle": "subtitles.block.chest.open"
},
"block.fence_gate.close": {
"sounds": [
"block/fence_gate/close1",
"block/fence_gate/close2"
],
"subtitle": "subtitles.block.fence_gate.toggle"
},
"block.fence_gate.open": {
"sounds": [
"block/fence_gate/open1",
"block/fence_gate/open2"
],
"subtitle": "subtitles.block.fence_gate.toggle"
},
"block.fire.ambient": {
"sounds": [
"fire/fire"
],
I expect a convenient way in order to edit "sounds" : [] []'s all at once.
The actual result is not really a convenient way and a time waster to edit all of the sounds [] values.

One way to do it is with regex, see regex101 demo.
Search for : ("sounds": \[\n)((\s*)[^\]]*\n)*(\s*\],)
Replace with: $1$3"test/test"\n$4

Related

How to fill/change particular unknown strings of some sections of a well formated JSON file from bash keeping the format intact?

How to fill/change particular unknown strings of some sections of a well formated .json file from bash keeping the format intact ?
Details:
Part of settings.json :
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles.
},
"theme": "dark",
"list":
[
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false,
"acrylicOpacity": 0.9,
"colorScheme" : "Material",
"cursorColor" : "#FFFFFD",
"useAcrylic" : true
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "Command Prompt",
"commandline": "cmd.exe",
"hidden": false,
"acrylicOpacity": 0.2,
"colorScheme" : "Frost",
"cursorColor" : "#000000",
"useAcrylic": true,
},
"schemes": [
{
"name" : "Frost",
"background" : "#ffffff",
"black" : "#3C5712",
"blue" : "#17b2ff",
"brightBlack" : "#749B36",
"brightBlue" : "#27B2F6",
"brightCyan" : "#13A8C0",
"brightGreen" : "#89AF50",
"brightPurple" : "#F2A20A",
"brightRed" : "#F49B36",
"brightWhite" : "#741274",
"brightYellow" : "#991070",
"cyan" : "#3C96A6",
"foreground" : "#000000",
"green" : "#6AAE08",
"purple" : "#991070",
"red" : "#8D0C0C",
"white" : "#6E386E",
"yellow" : "#991070"
},
another file pickcolor.txt:
#FFFAFA
#FFFAFA
#EEE9E9
#FFC1C1
#EEB4B4
#CDC9C9
#F08080
#FF6A6A
#CD9B9B
#EE6363
#BC8F8F
#FF4040
#FF3030
#EE3B3B
#CD5C5C
#CD5555
#EE2C2C
#8B8989
#CD3333
#FF0000
#FF0000
#8B6969
#CD2626
#EE0000
#B22222
#A52A2A
#CD0000
#8B3A3A
#8B2323
Want to create a bash which changes value of fields like "background", "cursorColor" to any random value from pickcolor.txt
Requirements/problems I am facing:
Fields appearing many times gets different values from each other
Value of fields changes from time to time, so simple find and replace do not work
Fields changes there line number every time new contents are added, so line number for fields do not remain same.
Format of .json file should not change
Please note : I do not want anyone to write the complete code ; just hinting/pointing out ways will be sufficient. And thanks in advance !!
I believe what you need is something like this
fields=(background cursorColor)
colors=("#FFFAFA" "#CDC9C9" "#EE3B3B")
size=${#colors[#]}
for f in "${fields[#]}"; do
for line in $(grep -n "\"$f\"" myfile.json | cut -f1 -d:); do
index=$(($RANDOM % $size))
color="${colors[$index]}"
sed -i $line's|\("'"$f"'"\s*:\s*"\).\+"|\1'"$color"'"|' myfile.json
done
done
There is some magic in this solution:
We choose random index from array of colors
For each of fields which should be replaced we create it's own unique sed expression, which
First part (\("'"$f"'"\s*:\s*"\)#.\+" finds anything matching pattern "<field name>"<spaces>:<spaces>"<any color>"
Captures anything until 3rd double quote into group
In second group (\1'"${colors[$index]}"'") replaces matched part with itself, but inserts randomly picked color from array into second double quote group
Weird things with lots of quotes are made to interpolate field name and color into single quotes
NB: this is tested with GNU sed. May need a bit different syntax for BSD

Mongo query to get comma separated value

I have query which is traversing only in forward direction.
example:
{
"orderStatus": "SUBMITTED",
"orderNumber": "785654",
"orderLine": [
{
"lineNumber": "E1000",
**"trackingnumber": "12345,67890",**
"lineStatus": "IN-PROGRESS",
"lineStatusCode": 50
}
],
"accountNumber": 9076
}
find({'orderLine.trackingNumber' : { $regex: "^12345.*"} })**
When I use the above query I get the entire document. But I want to fetch the document when I search with 67890 value as well
At any part of time I will be always querying with single tracking number only.
12345 or 67890 Either with 12345 or 67890. There are chances tracking number value can extend it's value 12345,56789,01234,56678.
I need to pull the whole document no matter what the tracking number is in whatever position.
OUTPUT
should be whole document
{
"orderStatus": "SUBMITTED",
"orderNumber": "785654",
"orderLine": [
{
"lineNumber": "E1000",
"trackingnumber": "12345,67890",
"lineStatus": "IN-PROGRESS",
"lineStatusCode": 50
}
],
"accountNumber": 9076
}
Also I have done indexing for trackingNumber field. Need help here. Thanks in advance.
Following will search with either 12345 or 67890. It is similar to like condition
find({'orderLine.trackingNumber' : { $regex: /12345/} })
find({'orderLine.trackingNumber' : { $regex: /67890/} })
There's also an alternative way to do this
Create a text index
db.order.createIndex({'orderLine.trackingnumber':"text"})
You can make use of this index to search the value from trackingnumber field
db.order.find({$text:{$search:'12345'}})
--
db.order.find({$text:{$search:'67890'}})
--
//Do take note that you can't search using few in between characters
//like the following query won't give any result..
db.order.find({$text:{$search:'6789'}}) //have purposefully removed 0
To further understand how $text searches work, please go through the following link.

Elastic search, watcher access dotted field names in the result set

I created a query for a elastic search watcher setup. The result set looks like this:
"_index": "transaction_broker-2017.09.15",
"_type": "transaction_broker",
"_id": "AV6Fn_UQ9KbnKce40avY",
"_score": 3.8539968,
"_source": {
"tbroker.workitem.sync_check.tbroker_value": 7000,
"source": "/logs/web/tomcat/tbroker.log.json",
"type": "transaction_broker",
"tbroker.job.instance_id": "lixporta-p00.xxxxxxx.15053054001381505305457198",
"tbroker.workitem.sync_check.backend_total_value": 6995,
"tbroker.appversion": "1.1.135-180",
"#version": 1,
"beat": {
"hostname": "lixporta-p00",
"name": "lixporta-p00",
"version": "5.1.1"
In the action section, I can access the fields by using:
"actions": {
"my-logging-action": {
"logging": {
"text": "There are {{ctx.payload.hits.hits.0._source.....
After the source tag, I use for example the "type" field from the list above. Other example is:
"ctx.payload.hits.hits.0._source.beat.hostname"
This works pretty fine...
But it is not possible to use a field like
"tbroker.workitem.sync_check.tbroker_value"
The parser thinks that this fields are nested, but this is only a fieldname with dots in it.
Is there any possiblity to "escape" this fieldname?
Anyone who also have had this problem ?
Many thanks & best regards
Claus
I think the following should work:
{{#ctx.payload.hits.hits.0._source}}{{tbroker.workitem.sync_check.tbroker_value}}{{/ctx.payload.hits.hits.0._source}}
It is a limitation of Mustache and this is a workaround.
Another example may help - when in a context looping through hits (I have added // comments purely for clarity - they aren't valid Mustache syntax & should be removed):
{{#ctx.payload.hits.hits}}
// This works fine
{{_source.foo}}
// Not working if each hit's _source contains "bar.baz", not nested "bar">"baz"
{{_source.bar.baz}}
{{/ctx.payload.hits.hits}}
Applying the same workaround by adding an extra context/section:
{{#ctx.payload.hits.hits}}
// Put us in the context of [the current hit] > _source
{{#_source}}
// Now both of these work...
{{foo}}
// ...including this one containing a dot (yay!)
{{bar.baz}}
{{/_source}}
{{/ctx.payload.hits.hits}}
There is no way to directly access source fields that have dots in them, but if you apply a transform like this:
"transform": {
"script": {
"inline": "return [ 'host' : ctx.payload.hits.hits[0]._source.host, 'tbroker_value' : ctx.payload.hits.hits[0]._source['tbroker.workitem.sync_check.tbroker_value']]",
"lang": "painless"
}
}
and then you can use {{ctx.payload.host}} and {{ctx.payload.tbroker_value}} in your action.

Collecting Data from Facebook posts using JPath / JSONPath

I have been at this for hours. How do you get filtering to work?
None of the solutions online works.
All I want to do is grab the comments(highlighted by these <<<>>>) from the json below from the name “Tori Smith”. I’ve been using this app to test http://jsonpath.curiousconcept.com/.
This is as far as I have gotten: ‘comments.data..from.id’
Data:
{
"id":"12029930209393029_10100748134340048",
"from":{
"id":"12029930209393029",
"name":"Tori Smith"
},
"message":"Buy this now",
"picture":"https:\/\/fbexternal-a.akamaihd.net\/app_full_proxy.php?app=141861192518680&v=1&size=z&cksum=a0471c1f5895cd22c74474fabc989c7e&src=http%3A%2F%2Fmedia3.policymic.com%2FYTM2OWUwN2Q0MSMvRnlpUTkxZU9DMWtGWFZ6TUNiYWh3RkxveXRjPS8yeDE6MTI4Nng2MjIvMTI4MHg2MjAvZmlsdGVyczpxdWFsaXR5KDcwKS9odHRwOi8vczMuYW1hem9uYXdzLmNvbS9wb2xpY3ltaWMtaW1hZ2VzL2JqNTdvbTZxZGd1N3ZpaGtvcWVrNnlzaTI5bW55dGZqanEwMWhuc3FqYjgxc3dkeGcyN2F6czV0eXV0bWJzZTguanBn.jpg",
"link":"http:\/\/mic.com\/articles\/101252\/this-tiny-box-will-let-you-stay-anonymous-on-the-internet",
"name":"This Tiny Box Will Let You Stay Anonymous on the Internet",
"caption":"Mic",
"description":"A simple and elegant solution to a major technology problem.",
"icon":"https:\/\/fbcdn-photos-d-a.akamaihd.net\/hphotos-ak-xpf1\/t39.2081-0\/10333103_752719651432828_1597152122_n.png",
"actions":[
{
"name":"Comment",
"link":"https:\/\/www.facebook.com\/12029930209393029\/posts\/10100748134340048"
},
{
"name":"Like",
"link":"https:\/\/www.facebook.com\/12029930209393029\/posts\/10100748134340048"
}
],
"privacy":{
"description":"Your friends",
"value":"ALL_FRIENDS",
"friends":"",
"networks":"",
"allow":"",
"deny":""
},
"type":"link",
"status_type":"app_created_story",
"application":{
"name":"Mic",
"namespace":"micmediaapp",
"id":"141861192518680"
},
"created_time":"2014-10-14T14:54:54+0000",
"updated_time":"2014-10-15T03:55:19+0000",
"comments":{
"data":[
{
"id":"10100748134340048_10100748984636048",
"from":{
"id":"123094958239849866",
"name":"Don Draper"
},
"message":"Even if I use Tor",
"can_remove":true,
"created_time":"2014-10-15T03:03:29+0000",
"like_count":0,
"user_likes":false
},
{
"id":"10100748134340048_10100749036726658",
"from":{
"id":"12029930209393029",
"name":"Tori Smith"
},
<<< "message":"Yes this can go with you and I think it works for all apps outside of TOR browser", >>>
"can_remove":true,
"created_time":"2014-10-15T03:55:19+0000",
"like_count":0,
"user_likes":false
},
{
"id":"10100748134340048_1010074901234658",
"from":{
"id":"12029930209393029",
"name":"Tori Smith"
},
<<< "message":"Second Text", >>>
"can_remove":true,
"created_time":"2014-10-15T03:55:19+0000",
"like_count":0,
"user_likes":false
}
],
"paging":{
"cursors":{
"after":"WTI5dGJXVnVkRjlqZFhKemIzSTZNVEF4TURBM05Ea3dNelkzTWpZMk5UZzZNVFF4TXpNME5UTXhPVG95",
"before":"WTI5dGJXVnVkRjlqZFhKemIzSTZNVEF4TURBM05EZzVPRFEyTXpZd05EZzZNVFF4TXpNME1qSXdPVG94"
}
}
}
}
To get at the comments you need to introduce a filter expression for filtering to comments by "Tori Smith" and then select the comment field.
Taking it step by step, to get all the comments, you need this:
$.comments.data
Then to filter to only the comments by "Tori Smith" add a filter like this:
$.comments.data[?(#.from.name == 'Tori Smith')]
Finally, to select only the message expand the query like this:
$.comments.data[?(#.from.name == 'Tori Smith')].message
I have tested this using the online JSON query tool here:
http://www.jsonquerytool.com/sample/jsonpathwhereselectcomments

Missing quotes in Json file

I have a very large Json file. It contains 27000 records.
a record looks like this:
{
adlibJSON: {
recordList: {
record: [
{
#attributes: {
priref: "4372",
created: "2011-12-09T23:09:57",
modification: "2012-08-11T17:07:51",
selected: "False"
},
acquisition.date: [
"1954"
],
acquisition.method: [
"bruikleen"
],
association.person: [
"Backer, Bregitta"
],
association.subject: [
"heraldiek"
],
collection: [
"Backer, collectie"
], ... ...
The problem is that this is not valid Json. The quotes are missing for the names.
Like for example acquisition.date should be "acquisition.date":
I need to edit this big json file and add all the quotation marks, otherwise the file doesn't parse with for example D3.js
What is the best way to repair this Json file?
I'd use a decent text editor with regex find and replace capability (e.g., Visual Studio, UltraEdit, etc.).
Then Do: find
^\s*(\w+\.\w+)\s*:
and replace with
"$1":
Or you could use powershell:
$allText = gc yourfile.txt
$allText -replace '^\s*(\w+\.\w+)\s*:', '"$1":'
If you can open it in a text editor, I think you can simply use a replace function for:
], --> ],"
and
: [ --> ": [
If your JSON is formatted the same throughout and doesn't contain those characters, this should work.
--
Note that you'll have to manually edit the first key yourself.