I have a json file with the following data:
{
"Index" : {
"example_user" : {
"important_key" : "leave_me_alone",
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
},
"another_user" : {
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
},
"extra_person" : {
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
}
}
}
As you can see, important_key is missing from another_user and extra_person.
My goal is to conditionally add "imporant_key" : "" where it is missing but not replace any existing important_key values. The program i use is JQ (1.6) from https://stedolan.github.io/jq/.
After many failed attempts on jqplay.org and having intensively (re)searched the internet on how to get the job done , i've only managed to realize the following:
Filter:.Index[]+={"important_key":"data"}
Result
{
"Index": {
"example_user": {
"important_key": "data",
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
}
},
"another_user": {
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
},
"important_key": "data"
},
"extra_person": {
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
},
"important_key": "data"
}
}
}
I'm aware += (over)writes any existing key value (note example_user). I've been experimenting with multiple piped filters/operators, including |= and { }, but in the end, the above result was the closest i could get.
(unfortunately i can't seem to find the jqplay code snippet where i went "crazy" with everything)
The moment you have to conditionally do something, you have to do some sort of filtering, either using if or select or other means.
For every object you wish to inspect, check if the object has your important_key and add it when necessary. If it has the key, do nothing, otherwise set it.
.Index[] |= if has("important_key") then . else .important_key = $myImportantKey end
Related
I want to tie a folder with multiple images to my data.
I have some data:
[
{
"title": "data1",
"images" : "./relimages/first-data/",
"cardimages" : "./relimages/main-page-card-images/1.jpg",
},
{
"title": "data2",
"images" : "./relimages/second-data/",
"cardimages" : "./relimages/main-page-card-images/2.jpg",
},
{
"title": "data3",
"images" : "./relimages/third-data/",
"cardimages" : "./relimages/main-page-card-images/3.jpg",
}
]
But when I query them with graphql, I only have a string : "./relimages/first-data"
Although, my cardimages data entry is seen as an image directly.
How can I make my query see that as a folder, instead of just returning the string.
I want this code to work:
<h1>{data.title}</h1>
data.images.edges.map(image => return (<img src={image.node.childImageSharp.fluid.src}/>))
Here's my current GraphQL query:
query flagquery {
allDataRoJson {
edges {
node {
images {
edges {
node {
childImageSharp {
fluid {
src
}
}
}
}
cardimages {
id
childImageSharp {
fluid {
originalImg
}
}
}
}
}
}
}
}
But my image data entry is seen as a string, instead of a folder. Is this possible somehow?
Error is:
Field \"images\" must not have a selection since type \"String\" has no subfields
I would like to convert this JSON
{
"l1k1": {
"l2k1": "l2v1",
"l2k2": 1
},
"l1k2": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
to this one
{
"papa": {
"l1k1c": {
"l2k1c": {
"string": "l2v1"
},
"l2k2c": {
"int": 1
}
},
"l1k2c": {
"array": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
}
}
where:
"l" stands for level
"k" for key, "v" for value
"e" for element
"c" for copy (where "*" maps to "*c")
I'm using circe's Json but having a hard time renaming the keys or creating parents or children with it. As I'm writing this, I'm thinking I may need to use its ACursor instead. As you may have guessed, I'm trying to generate an AVRO doc from an input JSON. I'm open to help w/ my approach or any suggestions about how to go about it in a cleaner way.
I have a json file which looks like this
{
"language":[
{
"lang":"English"
},
{
"lang":"Polish"
},
{
"lang":"German"
},
{
"lang":"Swedish"
},
{
"lang":"Dutch"
},
{
"lang":"Finnish"
},
{
"lang":"Turkish"
}
],
"currency":[
{
"curr" : "dollar"
},
{
"curr" : "pound"
},
{
"curr" : "rupees"
},
{
"curr" : "euro"
},
{
"curr" : "euro"
}
],
"gamename":[
{
"gname":"poker"
},
{
"gname":"slot"
}
],
"freeplay": "false"
}
I installed json-server-init globally and then ran watch command which threw the following error
Type of "freeplay" (string) in linkto.json is not supported. Use
objects or arrays of objects.
Can someone help me in understanding what is wrong or what did I do wrong?
From my understanding of json-server, the value of each key must be a valid JSON object, which is not the case for a simple string.
For example, change the value (contents of other keys omitted) to:
{
"language":[
...
],
"currency":[
...
],
"gamename":[
...
],
"freeplay": {
"enabled": "false"
}
}
if you'd like the request to:
http://localhost:3000/freeplay
to return:
{
"enabled": "false"
}
I am trying to create an index in Elasticsearch for the JSON file of format:
{ "index" : { "_index" : "entity", "_type" : "type1", "_id" : "0" } }
{ "eid":"guid of Event autogenerated", "entityInfo": { "entityType":"qualityevent", "defaultLocale":"en-US" }, "systemInfo": { "tenantId":"67" }, "attributesInfo" : { "jobId":"21", "matchStatus": "new" } }
{ "index" : { "_index" : "entity", "_type" : "type1", "_id" : "1" } }
{ "eid":"guid of Event autogenerated", "entityInfo": { "entityType":"qualityevent", "defaultLocale":"en-US" }, "systemInfo": { "tenantId":"67" }, "attributesInfo" : { "jobId":"20", "matchStatus": "existing" } }
I want the fields jobId and tenantId to be integers.
I am giving the following mapping in curl command:
curl -XPUT http://localhost:9200/entity -d '
{
"mappings": {
"entityInfo":
{
"properties" : {
"entityType" : { "type":"string","index" : "not_analyzed"},
"defaultLocale":{ "type":"string","index" : "not_analyzed"}
}
},
"systemInfo":
{
"properties" : {
"tenantId": { "type" : "integer" }
}
},
"attributesInfo" :
{
"properties" : {
"jobId": { "type" : "integer" },
"matchStatus": { "type":"string","index" : "not_analyzed"}
}
}
}
}
';
This does not give me an error. However, it creates new empty fields jobId and tenantId as integers and it keeps the existing data into attributesInfo.jobId as string. Same is the case with systemInfo.tenantId. I want to use these two fields in Kibana for visualization. I currently cannot use them as they are empty.
I am new to Kibana and Elasticsearch so I am not sure if the mapping is correct.
I have tried couple of other mappings as well but they give errors. Above mapping does not give error.
This is how Discover Tab on Kibana looks like: 1
Please let me know where I am going wrong.
I tried as you mentioned but it didn't help. What I realised after a lot of trial and error that my mapping was incorrect. I finally wrote the correct mapping and now it works correctly. Jobid and TenantId are recognised as numbers by Kibana. I am new to JSON, kibana, Bulk, Elastic so it took time to understand how mapping works.
If I have a JSON like so:
{
"data": [
{
"service" : { "id" : 1 }
},
{
"service" : { "id" : 2 }
},
{
"service" : {}
}
]
}
This query works:
$..service[?(#.id==2)]
And gives expected result:
[
{
"id" : 2
}
]
However, if I had strings as id's:
{
"data": [
{
"service" : { "id" : "a" }
},
{
"service" : { "id" : "b" }
},
{
"service" : {}
}
]
}
Running similar query:
$..service[?(#.id == "a")]
Gives no results (empty array).
I am using this evaluator.
I was looking at docs here but could not find anything to point me in the right direction... Any help if someone knows how to write such query? Thanks :)
without " works
$..service[?(#.id == b)]
give this result
[
{
"id" : "b"
}
]