Json.parse Syntax Error - json

I've got code.
{
s: "ok",
t: [1386493512, 1386493572, 1386493632, 1386493692],
c: [42.1, 43.4, 44.3, 42.8]
}
Why it return code
SyntaxError: JSON.parse: expected property name or '}' at line 2 column 4 of the JSON data

In javascript the keys are presumed to be strings so keys do not need to be in quotes, however in JSON they do. You should change your code to look like
{
"s": "ok",
"t": [1386493512, 1386493572, 1386493632, 1386493692],
"c": [42.1, 43.4, 44.3, 42.8]
}

Related

Change name of main row Rails in JSON

So i have a json:
{
"code": "Q0934X",
"name": "PIDBA",
"longlat": "POINT(23.0 33.0)",
"altitude": 33
}
And i want to change the column code to Identifier
The wished output is this
{
"Identifier": "Q0934X",
"name": "PIDBA",
"longlat": "POINT(23.0 33.0)",
"altitude": 33
}
How can i do in the shortest way? Thanks
It appears that both "the json" you have and your desired result are JSON strings. If the one you have is json_str you can write:
json = JSON.parse(json_str).tap { |h| h["Identifier"] = h.delete("code") }.to_json
puts json
#=> {"name":"PIDBA","longlat":"POINT(23.0 33.0)","altitude":33,"Identifier":"Q0934X"}
Note that Hash#delete returns the value of the key being removed.
Perhaps transform_keys is an option.
The following seems to work for me (ruby 2.6):
json = JSON.parse(json_str).transform_keys { |k| k === 'code' ? 'Identifier' : k }.to_json
But this may work for Ruby 3.0 onwards (if I've understood the docs):
json = JSON.parse(json_str).transform_keys({ 'code': 'Identifier' }).to_json

How to fetch an attribute value from a variable, having the content of a JSON response

I'm using the Robot Framework API automation. Here, storing the JSON response in a variable [POSTResp.content]. I.e., "POSTResp.content" has the whole response, as given below. Please help me to get an attribute's value (for ex, value of referenceId) from the stored content.
Example of JSON response:
{
"serviceResponseHeader": {
"responseContext": {
"responseCode": "MS19",
"responseDescription": "Success",
"serviceResponseTimeInGMT": "18 Sep 2018 16:12:43 GMT"
},
"requesterContext": {
"applicationCode": null,
"applicationSubCode": null,
"countryCode": null,
"requesterReferenceNumber": null,
"requestTimeInGMT": "30 Jun 2015 11:54:49 GMT",
"requesterUserIdentity": "23483",
"requesterGroupIdentity": "1620",
"requesterIpAddress": "",
"sessionIdentity": "2536kjhfdashfkhfsab",
"ssoSessionIdentity": "2536kjhfdashfkhfsab",
"requesterAbbreviatedGroupName": "NEWCOMP"
},
"serviceContext": {
"serviceVersionNumber": "1.0",
"serviceCode": "30"
}
},
"getProxyDetailResponseBody": {
"proxyDetails": {
"proxyType": "",
"proxyValue": "20140005K",
"referenceId": "PR18090000847597",
"transactionId": "18091801657466"
}
}
}
I've tried the below ways,
1) ${json} To JSON ${POSTResp.content} true
log to console \n the Proxy ID is ${json["proxyValue"]}
Result: Resolving variable '${json["proxyValue"]}' failed: TypeError: string indices must be integers, not str
2) ${json} Evaluate json.loads(${POSTResp.content}} json
log to console \n the Proxy ID is ${json["proxyValue"]}
Result: failed: SyntaxError: unexpected EOF while parsing (, line 1)
Issues with your two approaches:
1) the library keyword call passes a true argument (well, truth-like) to the pretty_print parameter:
${json} To JSON ${POSTResp.content} true
Looking at the library's source, in that case the keyword does not return a dict object - but a string, a beatified version of the source json. That coincides with the error your received.
Remove the "true" argument and it must return a dict.
2) In the Evaluate surround the variable with triple quotes (python's literal string):
${json} Evaluate json.loads('''${POSTResp.content}'''}
json
Without it, the framework just dumped the variable's value, which raised a python syntax error.
By the way, try not to make your variables with language keywords/library names - like ${json} up there.

Reading a json file into a RDD (not dataFrame) using pyspark

I have the following file: test.json >
{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
}
I want to load this file into a RDD. This is what I tried:
rddj = sc.textFile('test.json')
rdd_res = rddj.map(lambda x: json.loads(x))
I got an error:
Expecting object: line 1 column 1 (char 0)
I don't completely understand what does json.loads do.
How can I resolve this problem ?
textFile reads data line by line. Individual lines of your input are not syntactically valid JSON.
Just use json reader:
spark.read.json("test.json", multiLine=True)
or (not recommended) whole text files
sc.wholeTextFiles("test.json").values().map(json.loads)

Cant validate JSON when using different language. Error invalid characters found

First time im trying to create a JSON file,
Im trying to create a JSON file with different language other than English , but when i try to validate, it show Error Invalid characters found.
i tried this
{
"data": [
{
"id": "1",
"title": "Oru Velli Thaaram Vaana Veedhiyil",
"lyrics": "ഒരു വെള്ളിത്താരം വനവീഥിയിൽ തെളിയവേ
കുളിരീറൻ കാറ്റും കുഞ്ഞുതരാട്ട് മൂളവേ
ഇരുളിനലകൾ മൂടും ധരയിതിലൊരു ദീപം
കദനഭാരമെല്ലാം നീക്കിടുന്ന സ്നേഹം
പിറന്നു മണ്ണിലുഷസ്സിൻ ശോഭ പോലെ
(ഒരു വെള്ളിത്താരം…
മരുഭൂവിൽ അലയുമ്പോൾ ആ താരം മുൻപേ
മറയാതെ രാജക്കൾക്കതുമാർഗമായി
മരുഭൂവിൽ അലയുമ്പോൾ ആ താരം മുൻപേ
മറയാതെ രാജക്കൾക്കതുമാർഗമായി
പുൽക്കൂടും തേടിത്തേടി ബെത്ലഹേമിലവരണയുമ്പോൾ
ഗീതങ്ങൾ പാടിപ്പാടി വാനദൂതരും അണയുന്നൂ
തിരുസുതനെ കാണുംനേരം പാടുന്നു ഗ്ലോറിയ …
(ഒരു വെള്ളിത്താരം…
ശാരോനിൻ താഴ്വാരം തഴുകുന്ന കാറ്റെ
വരുമോ എൻ നാഥൻറെ അരികിൽ നീ മെല്ലെ
ശാരോനിൻ താഴ്വാരം തഴുകുന്ന കാറ്റെ
വരുമോ എൻ നാഥൻറെ അരികിൽ നീ മെല്ലെ
തഴുകൂ നിൻ വിരലാൽ നെറുകിൽ സ്നേഹനാഥനെ ആലോലം
പാടൂ നൽ ശ്രുതിയാൽ കാതിൽ സാന്ദ്രമാനന്ദ സംഗീതം
ഈ രാവിൽ പാരാകെ പാടുന്നു ഗ്ലോറിയ
(ഒരു വെള്ളിത്താരം…",
},
{
"id": "2",
"title": "Pukootil Vannu Jaathanayi",
"lyrics": "പുൽക്കൂട്ടിൽ വന്നു ജാതനായി
നക്ഷത്രം ഇന്ന് മിന്നി നിന്നു
ക്രിസ്മസ് രാവിൻറെ ഗാനമായി
വിണ്ണിൽ ആനന്ദമേളമായി താരകം ദീപമായ്
കൺകളിൽ തിളങ്ങി നിന്നു (2 )
ദൂതരാ വീണകൾ മീട്ടിടുന്നിതാ
ലോകരാ കീർത്തനം കേട്ടിടുന്നിതാ
ദേവദാരു പൂത്തു പാതിരാവു പെയ്തു
മഞ്ഞുതുള്ളി വീണവീഥി മിന്നിടുന്നു
( പുൽക്കൂട്ടിൽ)
വിദ്വരോ കാഴ്ചകൾ നല്കിടുന്നിതാ
വിന്നതിൽ നോക്കി സംപ്രീതരായിതാ
കീറ്റുശീല തന്നിൽ ദിവ്യശോഭ കണ്ടു
ആട്ടിടയരെത്തി ആർത്തു പാടിടുന്നു
( പുൽക്കൂട്ടിൽ)",
}
]
}
Error shows as INVALID JSON , Invalid characters found.
please help me to resolve this problem .
Problem Coming from
"lyrics": "ഒരു വെള്ളിത്താരം വനവീഥിയിൽ തെളിയവേ
Error Type
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
Reason
Line breaks inside your string . Encode with \n .A string is a sequence of zero or more Unicode characters .

Parsing complex json in pig?

I have json file in follwoing format:
{ "_id" : "foo.com", "categories" : [], "h1" : { "bar==" : { "first" : 1281916800, "last" : 1316995200 }, "foo==" : { "first" : 1281916800, "last" : 1316995200 } }, "name2" : [ "foobarl.com", "foobar2.com" ], "rep" : null }
So, how do i parse this json in pig..
also, the categories and rep can have some char in it..and might not be always empty.
I made the following attempt.
a = load 'sample_json.json' using JsonLoader('id:chararray,categories:[chararray], hostt:{ (variable_a: {(first:int,last:int)})}, ns:[chararray],rep:chararray ');
But i get this error:
org.codehaus.jackson.JsonParseException: Unexpected character ('D' (code 68)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream#4795b8e9; line: 1, column: 50]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:1582)
at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:386)
at org.apache.pig.builtin.JsonLoader.readField(JsonLoader.java:173)
at org.apache.pig.builtin.JsonLoader.getNext(JsonLoader.java:157)
at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigRecordReader.nextKeyValue(PigRecordReader.java:211)
at org.apache.hadoop.mapred.MapTask$NewTrackingRecordReader.nextKeyValue(MapTask.java:532)
at org.apache.hadoop.mapreduce.MapContext.nextKeyValue(MapContext.java:67)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:143)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:764)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:212)
You can use elephant bird pig jar for parsing json. It can parse all sort of json data.
Here are certain examples for parsing json via elephant bird pig using this jar.
https://github.com/twitter/elephant-bird/tree/master/examples/src/main/pig
It doesn't break even if an expected json tag isn't present.