Using regex in notepad++ to reduce json objects - json

I have a huge Json file containing objects of the form:
{ "country" : "UK", "city" : "London" }
I want to limit the number of instances for each "country". Can I do this using regex?
I want to remove the whole block of object that contains "UK".
I tried something like ^{.*"UK".*},
(starts with {
contains "UK"
ends with },)
But this is wrong and I can't figure out the correct way.
Any help would be appreciated.

In general, you should not be using regex alone to handle JSON content, even less so if that JSON be nested. If your JSON is always single level as you wrote above, then the following pattern might work:
\{[^}]*"UK"[^}]*\}
Replace the above by nothing, and do the find/replace in regex mode.
Demo

Related

How can I select all possible JSON Data in arrow syntax/JSON Extract in SQL

I need to be able to access all the available JSON data, the problem is that a lot of it is nested.
I currently have this query.
SELECT * FROM `system_log` WHERE entry->"$[0]" LIKE "%search_term%";
I need instead of entry->"$[0]", something like entry->"$*"
I think the arrow syntax is short for JSON_EXTRACT which I think would mean that a solution for extract would work for the arrow syntax.
{
" Name": {
"after": "Shop",
"before": "Supermarket"
}
}
This is an example of my JSON data and as you can see there are multiple levels to it meaning that entry->"$[0]" won't catch it.
version 8.0.19 of SQL
What I've tried so far is entry->"$[0]" and then prepending [0] after, but this solution does not seem very dynamic as the JSON data could get deeper and deeper.
JSON_SEARCH() won't work for the search you describe, because JSON_SEARCH() only searches for full string matches, not wildcards.
If you truly cannot predict the structure of your JSON, and you just want to find if the pattern '%search_term%' appears anywhere, then just treat the whole JSON document as a string, and use LIKE:
SELECT * FROM `system_log` WHERE entry LIKE "%search_term%";
If you have more specific search requirements, then you'll have to come up with a way to predict the path to the value you're searching for in your JSON document. That's something I cannot help you with, because I don't know your usage of JSON.

How to use regular expression in JSON as key

Is it even possible to do so? I didn't find a single solution on the internet. I'm working on Angular 6 project. I've to translate few strings and for that I'm maintaining one en-US.json file in i18n folder. JSON is like this:
"words": {
"Car": "Wagen",
"Ship": "Schiff",
"Flight": "Flug",
"*": "", // <------------ HELP NEEDED
}
I don't want to convert other strings whatsoever. Can I use regular expression to ignore them. Leaving them unattended is giving me strings like:
words.train
words.helicopter
Please correct me where I am wrong.

NIFI XML to JSON Null values

I am trying to run a very simple conversion flow that takes an input XML document and converts it to json using an Avro schema.
I must be doing something very wrong because I always get null values for even something simple like this:
<key1>value1</key1>
and the output is:
[{"key1":null}]
I followed this tutorial line by line here: https://pierrevillard.com/2018/06/28/nifi-1-7-xml-reader-writer-and-forkrecord-processor/
My AVRO Schema is defined as:
{
"type" : "record",
"name" : "MyClass",
"namespace" : "com.test.avro",
"fields" : [ {
"name" : "key1",
"type" : "string"
} ]
}
What am I missing? The only difference I can see is that my xml has a slightly different structure than the example. in the example, the XML seems to have structure of:
<key1 value="value1"/>
But even trying to change my input to that results in the same null values.
I see other posts on this questions but no real solutions. Even the some of the comments on those threads about incorrect XML structure, I have changed to that and it is still not working. I'm sure it is something simple, any help would be appreciated.
I'm very new to NIFI, but really like the potential of the tool!
Turns out that the NIFI component ConvertRecord expects XML data to not be at the root level, and instead the defined to start at the 2nd level of the XML document.
So my defined AVRO Schema as correct, I just had to wrap my input XML data in a root tag, like this:
<root><key1>value1</key1></root>

manipulating (nested) JSON keys and there values, using nifi

I am currently facing an issue where I have to read a JSON file that has mostly the same structure, has about 10k+ lines, and is nested.
I thought about creating my own custom processor which reads the JSON and replaces several matching key/values to the ones needed. As I am trying to use NiFi I assume that there should be a more comfortable way as the JSON-structure itself is mostly consistent.
I already tried using the ReplaceText processor as well as the JoltTransformJson processor, but I could not figure out. How can I transform both keys and values, if needed? For example: if there is something like this:
{
"id": "test"
},
{
"id": "14"
}
It might be necessary to turn the "id" into "Number" and map "test" to "3", as I am using different keys/values in my jsonfiles/database, so they need to fit those. Is there a way of doing so without having to create my own processor?
Regards,
Steve

Using regex to extract data from structured data

The problem I'm facing here is that I have a blob of text which contains structured data (in the form of a JSON payload) and I'm interested in extracting the value of one of the keys for a specific JSON instance, picture the structured data inside as the following:
"Item 1": {"key1":"item1_key1_value", "key2":"item1_key2_value", "key3":"item1_key3_value"}, "Item 2": {"key1":"item2_key1_value", "key2":"item2_key2_value", "key3":"item2_key3_value"}
What I would like to use is use regex to grab item1_key2_value for instance. The keys all have the same name but the items are different. So I know which key for which Item I need but am not quite sure of the regex to retrieve that value. I've tried a few approaches to some basic matching but was wondering if any other more experienced regex users could direct me a bit here and explain what I'm doing wrong
1(.)(?=item1_key2_value.) will match a chunk of data from here but I'm not sure of the best way to reduce it to the value that I need.
The regex syntax for JSON is clearly specified at http://www.json.org. If you scroll down a little to where it says "A string is a sequence of", you will find the proper string structure.
Assuming the string follows the correct JSON structure, you could use
"key2"\s*:\s*"((\\.|[^\\"])*)"
where \s means whitespace and * means 0 or more times. \\ means a slosh (backslash) character and can be followed by . (any character). If it does not encounter a slosh, then it instead looks for [^\\"], which means not slosh nor quote.
If you want to be a little more strict to the exact JSON form, you could try
"key2"\s*:\s*"((\\["\\/bfnrtu]|[^\\"])*)"
which you can see follows the string form on the webpage more closely.