How to use regular expression in JSON as key - json

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.

Related

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 in notepad++ to reduce json objects

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

Parsing translatable messages from JSON file

I have a project that I want to be localizable. While most strings are in the source code, where xgettext/Poedit can easily find them when wrapped with the localization function call, some are in pure JSON files, that I'm using for data storage. Since it's just JSON, and not actually JS, I can't use function calls. For example, a little database:
somedb.txt
[
{ "id": 1, "name": "Xyz", "local": "AxWhyZzz", /*...*/ },
/*...*/
]
Is there a way to extract the "local" values from the JSON files with xgettext? And if there isn't, what are my options? Creating a source file that has all local values, wrapped with calls to _?
Alternatively I could write my own parser of course, or modify gettext, but I'd much rather use existing solutions if available.
No, there isn't a way. JSON is just a generic container format, the actual meaning of the values is domain/application specific — and xgettext must understand the meaning to know what to extract. How could it understand your homegrown format?
For XML files, this is solved by ITS (v2), which gettext (and thus Poedit) supports since 0.19.7. But for JSON, nothing exists… yet. There's some work being done (see here and here and here), though.
Here is the way you will get them as JS arrays through XMLHttpRequest: http://codepen.io/KryptoniteDove/post/load-json-file-locally-using-pure-javascript
Also there is a way to include somedb.txt as a valid js if you modify it by adding variable id somevar to provide further access:
somevar = [
{ "id": 1, "name": "Xyz", "local": "AxWhyZzz", /*...*/ },
/*...*/
]

Pipe not renaming fields

I have a JSON feed that I want to rename some of the fields and output a new JSON feed.
Here is one of the nodes on the source JSON feed:
{
"SiteId": "A2390506",
"SiteName": "Blackford Drain",
"SubRegion": "Blackford Drain",
"SubRegionID": 38,
"ResultDate": "28/08/14 15:00",
"10.00": 0.0,
"100.00": 1.293,
"151.00": 4.8747,
"821.00": 13501.0
},
Its those latter fields that are labelled as floats (10.00, 100.00, etc) that I want to change to more meaningful names.
I thought this could be quite easily achieved with a Pipe and a Rename Operator.
I can rename any of the fields that have string names, but not the latter fields.
Have I done something wrong, or is this a limitation of the rename operator? Any ideas on a workaround?
The source of the pipe is here.
--EDIT
Should also point out that I have also tried 'item.100.00' which also fails to rename.

JSON interface with UNIX

Am very new to JSON interaction, I have few doubts regarding it. Below are the basic one
1) How could we call/invoke/open JSON file through Unix, I mean let suppose I have a metedata file in JSON, then how should I fetch/update the value backforth from JSON file.
2) Need the example, on how to interact it.
3) How Unix Shell is compatible to JSON, whether is there any other tech/language/tool which is better than shell script.
Thanks,
Nikhil
JSON is just text following a specific format.
Use a text editor and follow the rules. Some editors with "JSON modes" will help with [invalid] syntax highlighting, indenting, brace matching..
A "Unix Shell" has nothing directly to do with JSON - how does a shell relate to text or XML files?
There are some utilities for dealing with JSON which might be of use such as jq - but it really depends on what needs to be done with the JSON (which is ultimately just text).
Json is a format to store strings, bools, numbers, lists, and dicts and combinations thereof (dicts of numbers pointing to lists containing strings etc.). Probably the data you want to store has some kind of structure which fits into these types. Consider that and think of a valid representation using the types given above.
For example, if your text configuration looks something like this:
Section
Name=Marsoon
Size=34
Contents
foo
bar
bloh
EndContents
EndSection
Section
Name=Billition
Size=103
Contents
one
two
three
EndContents
EndSection
… then this looks like a list of dicts which contain some strings and numbers and one list of strings. A valid representation of that in Json would be:
[
{
"Name": "Marsoon",
"Size": 34,
"Contents": [
"foo", "bar", "bloh"
]
},
{
"Name": "Billition",
"Size": 103,
"Contents": [
"one", "two", "three"
]
},
]
But in case you know that each such dictionary has a different Name and always the same fields, you don't have to store the field names and can use the Name as a key of a dictionary; so you can also represent it as a dict of strings pointing to lists containing numbers and lists of strings:
{
"Marsoon": [
34, [ "foo", "bar", "bloh" ]
],
"Billition": [
103, [ "one", "two", "three" ]
]
}
Both are valid representations of your original text configuration. How you'd choose depends mainly on the question whether you want to stay open for later changes of the data structure (the first solution is better then) or if you want to avoid bureaucratic overhead.
Such a Json can be stored as a simple text file. Use any text editor you like for this. Notice that all whitespace is optional. The last example could also be written in one line:
{"Marsoon":[34,["foo","bar","bloh"]],"Billition":[103,["one","two","three"]]}
So sometimes a computer-generated Json might be hard to read and would need an editor at least capable of handling very long lines.
Handling such a Json file in a shell script will not be easy just because the shell has no notion of the idea of such complex types. The most complicated it can handle properly is a dict of strings pointing to strings (bash arrays). So I propose to have a look for a more suitable language, e. g. Python. In Python you can handle all these structures quite efficiently and with very readable code:
import json
with open('myfile.json') as jsonFile:
data = json.load(jsonFile)
print data[0]['Contents'][2] # will print "bloh" in the first example
# or:
print data['Marsoon'][1][2] # will print "bloh" in the second example