Reading json file from remote url in groovy - json

Suppose Some JSON file is # www.github.com/xyz/Hello.json, I want to read this content of this JSON in JSON object in Groovy

You then need:
import groovy.json.JsonSlurper
def slurped = new JsonSlurper().parse('www.github.com/xyz/Hello.json'.toURL())
Here you can find more info.

Related

How can I get data from external API JSON file in ODOO

[{"Id":"605a321e-7c10-49e4-9d34-ba03c4b34f69","Url":"","Type":"INBOUND_OUTBOUND",
"ClearCurrentData":true,"FillOutFormFields":true,"RequestProtocol":"HTTP_REQUEST",
"FileToSend":"NONE","SendDefaultData":false,"SendDetectionData":false,"ShowDialogMessage":false,"IsActive":false,"SendingTrigger":"MANUALLY","TCPSocketMethod":"","TriggerButtonName":"Get data"}]
This is an External API Call JSON file how can i get the data in ODOO Any Solutions please ?
Mentioned Above code API JSON file
i had only External JSON File,don't have a table name & Database name is it possible to get data in ODOO
create a model for this data for example
class apiCall(models.Model):
_name = 'apiCall.api'
id = fields.Char(string='Id')
url = fields.Char(string='url')
#api.model
def call_api(self):
### result of calling external api is "result"
for line of self:
id = result[0].Id
url = result[0].url
You can call a method on button click and access the json file. Below is the code for accessing json file:-
import json
def api_call(self):
# Opening JSON file
f = open('file_name.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in self:
print(i)
# Closing file
f.close()
Below is the button code which is applied in xml file:-
<button name="api_call" type="object" string="Call API" class="oe_highlight" />
by this way you can call the json file with external api in odoo.
In your custom module (my_custom_module), move your json file to its subdirectory static/src:
import json
from odoo.modules.module import get_module_resource
def call_api(self)
json_filepath = get_module_resource('my_custom_module', 'static/src/', 'my_api_cred.json')
with open(json_filepath) as f:
data = json.load(f)

Making json files in Scala

I am working on a Scala app. I have a method that gives me JSON and I convert it to string using toString as follows:
def myjson(fileName:String){
val myJson = myData.getJsonData().toString
}
Here getJsonData() will give me a .json. I want to write this .json into a file and save this .json in resources section in my project. Format of the file should be ".json". Name of the file is the one which I am getting in above method. How can I do that?
One approach would be as follow
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
def myjson(fileName:String){
val myJson = myData.getJsonData().toString
val path = s"/user/myProject/..../resources/$fileName"
Files.write(Paths.get(path), myJson.getBytes(StandardCharsets.UTF_8))
}
I understand that fileName: String will be something like filename.json

How to save JSON data fetched from URL in PySpark?

I have fetched some .json data from API.
import urllib2
test=urllib2.urlopen('url')
print test
How can I save it as a table or data frame? I am using Spark 2.0.
This is how I succeeded importing .json data from web into df:
from pyspark.sql import SparkSession, functions as F
from urllib.request import urlopen
spark = SparkSession.builder.getOrCreate()
url = 'https://web.url'
jsonData = urlopen(url).read().decode('utf-8')
rdd = spark.sparkContext.parallelize([jsonData])
df = spark.read.json(rdd)
For this you can have some research and try using sqlContext. Here is Sample code:-
>>> df2 = sqlContext.jsonRDD(test)
>>> df2.first()
Moreover visit line and check for more things here,
https://spark.apache.org/docs/1.6.2/api/python/pyspark.sql.html
Adding to Rakesh Kumar answer, the way to do it in spark 2.0 is:
http://spark.apache.org/docs/2.1.0/sql-programming-guide.html#data-sources
As an example, the following creates a DataFrame based on the content of a JSON file:
# spark is an existing SparkSession
df = spark.read.json("examples/src/main/resources/people.json")
# Displays the content of the DataFrame to stdout
df.show()
Note that the file that is offered as a json file is not a typical JSON file. Each line must contain a separate, self-contained valid JSON object. For more information, please see JSON Lines text format, also called newline-delimited JSON. As a consequence, a regular multi-line JSON file will most often fail.
from pyspark import SparkFiles
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("Project").getOrCreate()
zip_url = "https://raw.githubusercontent.com/spark-examples/spark-scala-examples/master/src/main/resources/zipcodes.json"
spark.sparkContext.addFile(zip_url)
zip_df = spark.read.json("file://" +SparkFiles.get("zipcodes.json"))
#click on raw and then copy url

How to read and ouput Json file with groovy

I am trying to: 1. read a json file with groovy; 2. change the object a little bit; 3. output/override as a new json file.
so far, I know we can use import groovy.json.JsonSlurper;, but I searched some samples, all for parse the .txt file (convert txt to json format object) like this:
def inputFile = file('json_input/' + fileName)
def inputJson = new JsonSlurper().parseText(inputFile.text)
print out...
but not read json file directly.
Is there any simple way to read and get the json object from a .json file so that I can make some changes of the data?
Thanks!

Reading json content from file to do assertion functional testing in SOUP UI using Groovy scripts

I am using SOAP UI to test the RESTful web services. To automate the regression testing, I need to compare the actual response and expected output. I have the actual response to the request parsed into a json object. I have the expected output in a text file with JSON content.
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def responseAsJsonObject = slurper.parseText response
//? Read json file content and parse it as an json object
assert fileContentAsJsonObject == responseAsJsonObject
I need a way to read this JSON content from the text file and parse into a json object in Groovy.
def fileContent = new File('D:\\test.json').text
def fileContentAsJsonObject = slurper.parseText fileContent
It works! Get the file content using .text and parse the fileContent to the jsonSlurper, which generates you the json object.
Its better to use log.info to write on log console and debugg.