C# Trying to split a string to get json object value
You are calling reader.Readline()
twice: once for the comparison and then again inside your loop. You are actually skipping every other line. And what is probably happening is that you are reaching the end of your file and then calling reader.Readline()
again, which is null. Try this instead:
line = reader.ReadLine();
while (line != null)
{
string[] words = JsonSplitString(line);
string json = words[1];
writer.WriteLine("{0}", json);
line = reader.ReadLine();
}
how to split text of a JSON file into pieces using C#
So for just convert JSON String to array string, you need to split the string with this code :
string jsonString= "{your json string}";
string[] MyObjects = jsonString.Split(',');
You can also parse it with JSON Parsers like NewtonJson or the default one!
var MyObjects = JsonConvert.DeserializeObject <Any type you want> (jsonData);
OR Using JObject:
var MyObjects = JObject.Parse(jsonData);
Or Use Default one:
var MyObjects = (any type you want)serializer.DeserializeObject(jsonData);
split json string into its constituent 1st child nodes and put keys into string array
you could do,
IEnumerable<string> ParsePropertyNames(string s)
{
var o = JObject.Parse(s);
return o.Properties().Select(p => p.Name);
}
which your could use like,
var results = ParsePropertyNames(yourString).ToArray();
How to extract the string values from json array and store it
I would have thought you would need to get the value first:
let str = pm.response.json()[0].stringval
Then split that on the pipe:
let value = str.split('|')
Then store the values as variables:
pm.environment.set('value_1', value[0])
pm.environment.set('value_2', value[1])
pm.environment.set('value_3', value[2])
I've not run any of this so I would take each thing at a time and log it to the console to ensure that it's captured the right data points before putting it all together.
Split Json object into multiple json objects in PHP
Try this loop over each record and save it in result.
$json=json_decode($str,true);
array_map(function($value) use (&$results){
$results[$value['id']]=json_encode($value['items']);
return $results;
}
,$json['results']);
print_r($results);
output
Array (
[001] => {"item11":"value1","item12":"value2","item13":"value3"}
[002] => {"item21":"value1","item22":"value2","item23":"value3"}
[003] => {"item31":"value1","item32":"value2","item33":"value3"} )
Split values from many columns accordingly over multiple rows
You may try to transform the values in the L_VALUE
, H_VALUE
and UNIT
columns as JSON (-10;25
into ["-10", "-25"]
) and parse the values with additional OPENJSON()
call. The result from the second OPENJSON()
is a table with columns key
, value
and type
and in case of an array, the key
column contains the index of each item in the JSON array, so you need an appropriate JOIN
s:
Table and JSON:
DECLARE @JsonData NVARCHAR(MAX);
SET @JsonData = N'[
{"id": 1, "lval": "-10;15", "hval": "-20;45", "unit": "kg;m"},
{"id": 2, "lval": "-10;15;13", "hval": "-20;45;55", "unit": "kg;m;cm"},
{"id": 3, "lval": "-10", "hval": "-20", "unit": "kg"}
]';
DECLARE @ExampleTable TABLE (
EQ BIGINT,
L_VALUE NVARCHAR(100),
H_VALUE NVARCHAR(100),
UNIT NVARCHAR (30)
)
Statement:
INSERT INTO @ExampleTable
SELECT j.[EQ], a.[L_VALUE], a.[H_VALUE], a.[UNIT]
FROM OPENJSON(@JsonData) WITH (
[EQ] BIGINT 'strict $.id',
[L_VALUE] NVARCHAR(100) '$.lval',
[H_VALUE] NVARCHAR(100) '$.hval',
[UNIT] NVARCHAR(20) '$.unit'
) j
CROSS APPLY (
SELECT l.[value], h.[value], u.[value]
FROM OPENJSON(CONCAT('["', REPLACE(j.L_VALUE, ';', '","'), '"]')) l
JOIN OPENJSON(CONCAT('["', REPLACE(j.H_VALUE, ';', '","'), '"]')) h ON l.[key] = h.[key]
JOIN OPENJSON(CONCAT('["', REPLACE(j.UNIT, ';', '","'), '"]')) u ON l.[key] = u.[key]
) a (L_VALUE, H_VALUE, UNIT)
Result:
EQ L_VALUE H_VALUE UNIT
----------------------
1 -10 -20 kg
1 15 45 m
2 -10 -20 kg
2 15 45 m
2 13 55 cm
3 -10 -20 kg
Related Topics
How Avoid Adding Duplicates to Database Managed by Entityframework Caused by Seed Method
Checking Whether a Number Contains Numbers 1 to N as Factors
Linq - Groupby a Key and Then Put Each Grouped Item into Separate 'Buckets'
How to Open in Default Browser in C#
Updating an Object from a List in C#
Linq to Find All Childs Related to Parents Id
How to Download Image from Url
C# - Comparing Items Between 2 Lists
How to Dynamically Adjust Table Column Width in Rdlc Report
Append a Lists Contents to Another List C#
Error: the Entity Type Requires a Primary Key
Error 5:Access Denied When Starting Windows Service
Reportviewer: Show Reports in Print Layout With Page Width Zoommode
How to Convert a Dictionary to a Json String in C#
How to Check If Multiple Values in an Array Are Equal to One String