Expecting 'EOF', '}', ',', ']', got 'STRING' - json

I am trying to add some code but it turns out I cant add these two together, I am new to JSON and coding in general, anyhelp?
"playerStatProgression": [{
"name": "HalloweenKillerVialRepetitions"
}, {
"name": "HalloweenSurvivorVialRepetitions",
"value": 1
}, {
"name": "DBD_HalloweenSurvivorToxin"
}],
"specialEvent": [{
"eventId": "Halloween2018",
"seenCinematics": [0]
}],
"versionNumber": 7
}
"playerStatProgression": [{
"name": "DBD_GoldenCoin",
"value": 89
}, {
"name": "DBD_BurntCoin",
"value": 53
}, {
"name": "DBD_SummerKillerCoin",
"value": 71
}, {
"name": "DBD_SummerSurvivorCoin",
"value": 112
}], "versionNumber": 7
}

You're supposed to wrap your JSON object in curly braces ({}) because a JSON file can only contain one object. Also, it seems invalid. Put it through a validator to figure out the issues.

You can do this in this way:
[
{
"playerStatProgression": [
{
"name": "HalloweenKillerVialRepetitions"
},
{
"name": "HalloweenSurvivorVialRepetitions",
"value": 1
},
{
"name": "DBD_HalloweenSurvivorToxin"
}
],
"specialEvent": [
{
"eventId": "Halloween2018",
"seenCinematics": [
0
]
}
],
"versionNumber": 7
},
{
"playerStatProgression": [
{
"name": "DBD_GoldenCoin",
"value": 89
},
{
"name": "DBD_BurntCoin",
"value": 53
},
{
"name": "DBD_SummerKillerCoin",
"value": 71
},
{
"name": "DBD_SummerSurvivorCoin",
"value": 112
}
],
"versionNumber": 7
}
]

Related

Cannot get jq to query json object [duplicate]

This question already has answers here:
How to use jq when the variable has reserved characters?
(3 answers)
Closed 6 months ago.
I have a JSON file that I am trying to query with jq. I am unable to retrieve the observations. I am trying to retieve each of the "observations using the following command and not able to get to the result:
cat sample3.json | jq .dataSets[0].series.0:0:0:0:0.observations.0[0]
I am able to retieve up to the series using:
cat sample3.json | jq .dataSets[0].series
But once I try to drill down further I am getting a compile error:
$ cat sample3.json | jq .dataSets[0].series.0:0:0:0:0
jq: error: syntax error, unexpected LITERAL, expecting end of file (Unix shell quoting issues?) at <top-level>, line 1:
.dataSets[0].series.0:0:0:0:0
jq: 1 compile error
I am not sure what I am doing wrong here....
The input file is:
{
"header": {
"id": "b8be2cd5-33bf-4687-9e81-eb032f6f8a71",
"test": false,
"prepared": "2022-09-01T13:30:57.013+02:00",
"sender": {
"id": "ECB"
}
},
"dataSets": [
{
"action": "Replace",
"validFrom": "2022-09-01T13:30:57.013+02:00",
"series": {
"0:0:0:0:0": {
"attributes": [
0,
null,
0,
null,
null,
null,
null,
null,
null,
null,
null,
null,
0,
null,
0,
null,
0,
0,
0,
0
],
"observations": {
"0": [
1.4529,
0,
0,
null,
null
],
"1": [
1.4472,
0,
0,
null,
null
],
"2": [
1.4591,
0,
0,
null,
null
]
}
}
}
}
],
"structure": {
"links": [
{
"title": "Exchange Rates",
"rel": "dataflow",
"href": "https://sdw-wsrest.ecb.europa.eu:443/service/dataflow/ECB/EXR/1.0"
}
],
"name": "Exchange Rates",
"dimensions": {
"series": [
{
"id": "FREQ",
"name": "Frequency",
"values": [
{
"id": "D",
"name": "Daily"
}
]
},
{
"id": "CURRENCY",
"name": "Currency",
"values": [
{
"id": "AUD",
"name": "Australian dollar"
}
]
},
{
"id": "CURRENCY_DENOM",
"name": "Currency denominator",
"values": [
{
"id": "EUR",
"name": "Euro"
}
]
},
{
"id": "EXR_TYPE",
"name": "Exchange rate type",
"values": [
{
"id": "SP00",
"name": "Spot"
}
]
},
{
"id": "EXR_SUFFIX",
"name": "Series variation - EXR context",
"values": [
{
"id": "A",
"name": "Average"
}
]
}
],
"observation": [
{
"id": "TIME_PERIOD",
"name": "Time period or range",
"role": "time",
"values": [
{
"id": "2022-08-29",
"name": "2022-08-29",
"start": "2022-08-29T00:00:00.000+02:00",
"end": "2022-08-29T23:59:59.999+02:00"
},
{
"id": "2022-08-30",
"name": "2022-08-30",
"start": "2022-08-30T00:00:00.000+02:00",
"end": "2022-08-30T23:59:59.999+02:00"
},
{
"id": "2022-08-31",
"name": "2022-08-31",
"start": "2022-08-31T00:00:00.000+02:00",
"end": "2022-08-31T23:59:59.999+02:00"
}
]
}
]
},
"attributes": {
"series": [
{
"id": "TIME_FORMAT",
"name": "Time format code",
"values": [
{
"name": "P1D"
}
]
},
{
"id": "BREAKS",
"name": "Breaks",
"values": []
},
{
"id": "COLLECTION",
"name": "Collection indicator",
"values": [
{
"id": "A",
"name": "Average of observations through period"
}
]
},
{
"id": "COMPILING_ORG",
"name": "Compiling organisation",
"values": []
},
{
"id": "DISS_ORG",
"name": "Data dissemination organisation",
"values": []
},
{
"id": "DOM_SER_IDS",
"name": "Domestic series ids",
"values": []
},
{
"id": "PUBL_ECB",
"name": "Source publication (ECB only)",
"values": []
},
{
"id": "PUBL_MU",
"name": "Source publication (Euro area only)",
"values": []
},
{
"id": "PUBL_PUBLIC",
"name": "Source publication (public)",
"values": []
},
{
"id": "UNIT_INDEX_BASE",
"name": "Unit index base",
"values": []
},
{
"id": "COMPILATION",
"name": "Compilation",
"values": []
},
{
"id": "COVERAGE",
"name": "Coverage",
"values": []
},
{
"id": "DECIMALS",
"name": "Decimals",
"values": [
{
"id": "4",
"name": "Four"
}
]
},
{
"id": "NAT_TITLE",
"name": "National language title",
"values": []
},
{
"id": "SOURCE_AGENCY",
"name": "Source agency",
"values": [
{
"id": "4F0",
"name": "European Central Bank (ECB)"
}
]
},
{
"id": "SOURCE_PUB",
"name": "Publication source",
"values": []
},
{
"id": "TITLE",
"name": "Title",
"values": [
{
"name": "Australian dollar/Euro"
}
]
},
{
"id": "TITLE_COMPL",
"name": "Title complement",
"values": [
{
"name": "ECB reference exchange rate, Australian dollar/Euro, 2:15 pm (C.E.T.)"
}
]
},
{
"id": "UNIT",
"name": "Unit",
"values": [
{
"id": "AUD",
"name": "Australian dollar"
}
]
},
{
"id": "UNIT_MULT",
"name": "Unit multiplier",
"values": [
{
"id": "0",
"name": "Units"
}
]
}
],
"observation": [
{
"id": "OBS_STATUS",
"name": "Observation status",
"values": [
{
"id": "A",
"name": "Normal value"
}
]
},
{
"id": "OBS_CONF",
"name": "Observation confidentiality",
"values": [
{
"id": "F",
"name": "Free"
}
]
},
{
"id": "OBS_PRE_BREAK",
"name": "Pre-break observation value",
"values": []
},
{
"id": "OBS_COM",
"name": "Observation comment",
"values": []
}
]
}
}
}
The .foo syntax cannot be used if the key name has anything but alphanumeric characters or the underscore, or if the first character of the key name is numeric.
Assuming you are using a recent version of jq,
you can always use the form: ."foo", which is actually an abbreviation of the basic form, .["foo"].
So assuming you're using a sufficiently recent version of jq, your query could begin with:
.dataSets[0].series."0:0:0:0:0"
If you are presenting the jq query on a command line, then you may have to escape the double-quotes appropriately, e.g. in a bash shell, by enclosing the jq query in single-quotes.

Import JSON with objects as nested to Elastic Search

i've log with thousands records of aggregated data in JSON:
{
"count": 25,
"domain": "domain.tld",
"geoips": {
"AU": 5,
"NZ": 20
},
"ips": {
"1.2.3.4": 5,
"1.2.3.5": 1,
"1.2.3.6": 1,
"1.2.3.7": 1,
"1.2.3.8": 1,
"1.2.3.9": 9,
"1.2.3.10": 7
},
"subdomains": {
"a.domain.tld": 1,
"b.domain.tld": 1,
"c.domain.tld": 1,
"domain.tld": 22
},
"tld": "tld",
"types": {
"1": 3,
"43": 22
}
}
and i have mapping on ES:
"mappings": {
"properties": {
"count": {
"type": "long"
},
"domain": {
"type": "keyword"
},
"ips": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"val": {
"type": "long"
}
}
},
"geoips": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"val": {
"type": "long"
}
}
},
"subdomains": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"val": {
"type": "long"
}
}
},
"tld": {
"type": "keyword"
},
"types": {
"type": "nested",
"properties": {
"key": {
"type": "keyword"
},
"val": {
"type": "long"
}
}
}
}
}
Is there any simple way how import these lines to ES as nested objects ? If i use a bulk insert without modification, the ES will modify mapping by adding a new field for each IP/subdomain/GeoIP instead add it as simple key/val object.
Or only one way is regenerate JSON to key/val nested fields ?
Your mapping is already very good but the data doesn't fit it since the nested data type expects an array of objects, not a single object. So you'll need to transform your nested objects into array of key-value pairs like so:
...
"ips": [
{
"key": "1.2.3.4",
"val": 5
},
{
"key": "1.2.3.5",
"val": 1
},
...
],
"subdomains": [
{
"key": "a.domain.tld",
"val": 1
},
{
"key": "b.domain.tld",
"val": 1
},
...
]
...

Jsonpath querying only one item

I have a problem to find out only a determined value in a json using jsonpath.
I have this json:
{"tvs": {
{ "tv": [
{
"serial": "HD1300",
"data": [
{
"title": "manufacturer",
"value": "lg"
},
{
"title": "color",
"value": "silver"
},
{
"title": "inches",
"value": 32
},
{
"title": "connection",
"value": 220
},
{
"title": "connection",
"value": 400
}
]
}.. more tvs
And I want to know if the value connection:400 is present for serial hd1300
I already tried with:
$.tvs.[?(#.serial=='hd1340')].data.[?(#.title== 'connection'),(#.value==400)]
But my problem is that I retrieve also the "connection" with 200. How can I filter to get only this value?
I think you might have an error in your JSON (extra brace between tvs and tv). I was able to get this to work on http://jsonpath.com.
{"tvs":
{ "tv": [
{
"serial": "HD1300",
"data": [
{
"title": "manufacturer",
"value": "lg"
},
{
"title": "color",
"value": "silver"
},
{
"title": "inches",
"value": 32
},
{
"title": "connection",
"value": 220
},
{
"title": "connection",
"value": 400
}
]}
]}
}
$.tvs.tv.[?(#.serial=='HD1300')].data.[?(#.title=='connection' && #.value=='400')]

What is my JSON file consist of?

I have read tutorials on the syntax of JSON files but I am still not quite sure I got it. Here is my JSON file:
{
"dataset": [
{
"seriesname": "Item1 Price",
"data": [
{
"value": 4.72
},
{
"value": 2.81
},
{
"value": 6.18
},
{
"value": 5.17
},
{
"value": 2.94
},
{
"value": 3.77
},
{
"value": 1.7
},
{
"value": 6.72
},
{
"value": 4.61
}
]
},
{
"seriesname": "Item2 Price",
"data": [
{
"value": 2.49
},
{
"value": 0.72
},
{
"value": 4.06
},
{
"value": 1.74
},
{
"value": 7.23
},
{
"value": 5.83
},
{
"value": 2.59
},
{
"value": 7.54
},
{
"value": 7.02
}
]
}
],
"categories": [
{
"label": "Jan"
},
{
"label": "Feb"
},
{
"label": "Mar"
},
{
"label": "Apr"
},
{
"label": "May"
},
{
"label": "Jun"
},
{
"label": "Jul"
},
{
"label": "Aug"
},
{
"label": "Sept"
}
]
}
From what I understand, there are 2 JSONObjects here, one is "dataset" and another is "categories". The "dataset" object has 2 JSONObjects inside it (Item1 Price and Item2Price) and they both are arrays which have several values. "categories" is an array with several entries. Am I correct or am I misreading the file?
You're correct. You can use any of the JSON visualizers out there (just google that term) to 'see' the data structure of the JSON array...
for example the one I use often is located here.
if you paste your JSON data in the left column, then click the arrow in the middle pointing to the right, it will let you navigate and explore the data easily (see screenshot below).
When I started working with JSON it really helped me visualize the syntax and structure of some complex data I was working with.
Good luck!

jsonpath query for reading json's child elements

I'm trying to read element's values.
For test purposes I was trying to read account_name element value.
I've tried this
jsonloop query = "data/ads/data/insights/data[*]"
account_name = "$.data.ads.data.insights.data.[*].account_name"
But not getting result. Apart from this I've googled alot and my logic seems to be okay..but I dont know where am doing wrong.
{
"data": [{
"id": "act_78425484545145418"
}, {
"id": "act_87814545415645416"
}, {
"ads": {
"data": [{
"insights": {
"data": [{
"account_name": "Emirates WW",
"campaign_name": "FR_Ozone_aug",
"adset_name": "Android",
"ad_name": "C_mail_9sept",
"spend": 12.2,
"impressions": "1146",
"clicks": "16",
"_store_clicks": "0",
"inline_click_pointss": "16",
"actions": [{
"action_type": "custom_event._activate_",
"value": 2
}, {
"action_type": "custom_event.other",
"value": 1
}, {
"action_type": "click_points",
"value": 16
}, {
"action_type": "__install",
"value": 1
}, {
"action_type": "offsite_points.view_content_points",
"value": 25
}, {
"action_type": "post_points",
"value": 16
}, {
"action_type": "post_points",
"value": 16
}, {
"action_type": "offsite_points",
"value": 25
}, {
"action_type": "custom_event",
"value": 3
}],
"date_start": "2016-09-09",
"date_stop": "2016-09-19"
}],
"paging": {
"cursors": {
"before": "ADZZZZ",
"after": "ADZZZZ"
}
}
},
"id": "6054027758549"
}, {
"insights": {
"data": [{
"account_name": "Emirates WW",
"campaign_name": "FR_Ozone_aug",
"adset_name": "Reta_Phy_iOS",
"ad_name": "Council_email_9sept",
"spend": 10.13,
"impressions": "1004",
"clicks": "10",
"_store_clicks": "8",
"inline_click_pointss": "10",
"actions": [{
"action_type": "click_points",
"value": 10
}, {
"action_type": "offsite_points.view_content_points",
"value": 38
}, {
"action_type": "post_points",
"value": 10
}, {
"action_type": "post_points",
"value": 10
}, {
"action_type": "offsite_points",
"value": 38
}],
"date_start": "2016-09-09",
"date_stop": "2016-09-19"
}],
"paging": {
"cursors": {
"before": "ADZZZZ",
"after": "ADZZZZ"
}
}
},
"id": "6054030075149"
}],
"paging": {
"cursors": {
"before": "Juysubgysbyy",
"after": "Nhwunhseuubeyegb"
}
}
}
}]
}
Can anyone help ?
Thanks in Advance !!
First and second data properties have array values, so you should add .[*] after them:
account_name = "$.data.[*].ads.data.[*].insights.data.[*].account_name"
The result:
[
"Emirates WW",
"Emirates WW"
]