question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception while accessing into flattened json object

See original GitHub issue

I’m trying to access flattened json object by using json path:

using ChoETL;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		typeof(ChoJSONReader).GetAssemblyVersion().Print();
		"".Print();
		
		string json = @"{
  ""BrandId"": ""998877665544332211"",
  ""Categories"": [ ""112233445566778899"" ],
  ""Contact"": {
    ""Phone"": [
      {
        ""Value"": ""12346789"",
        ""Description"": { ""vi"": ""Phone"" },
        ""Type"": 1
      }
    ]
  }
}";
		using (var r = ChoJSONReader.LoadText(json)
			   .Configure(c => c.DefaultArrayHandling = false)
			   .Configure(c => c.FlattenNode = true)
			   .Configure(c => c.UseNestedKeyFormat = true)
			   .Configure(c => c.FlattenByNodeName = "Contact.Phone")
			   .Configure(c => c.NestedKeySeparator = '.')
			   .Configure(c => c.NestedColumnSeparator = '.')
			  )
		{
			//var dt = r.AsDataTable();
			//dt.DumpAsJson().Print();
			var a = r.WithField("Phone","$.[\"Contact.Phone.Value\"]");
			a.Print();
			
		}

	}
}

After running this code, it’s return this output:

ChoETL.JSON.Core v1.2.1.45

[
  [ ]
]

Expected output:

[
"12346789"
]

I’m using ChoETL.JSON.Core v1.2.1.45 beta1 https://dotnetfiddle.net/WJHC2P @Cinchoo

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
zhenyuan0502commented, Mar 30, 2022

I understand what you have done. But when I access to ""Description"": { ""vi"": ""Phone"" } by using

var a = r.WithField("Phone","$.[\'Contact.Phone.Description.vi\']", isArray: false);

it’s return null value @Cinchoo https://dotnetfiddle.net/1fGp7O

Because it was a key separator, if you change . to _ as .Configure(c => c.NestedKeySeparator = '_'), you don’t need to wrapper by brackets, so it is just simply var a = r.WithField("Phone","$.Contact_Phone_Description.vi", isArray: false);

1reaction
Cinchoocommented, Mar 29, 2022

jsonpath contains special character, hence must be given in $.['valid.key.with.dot'] format.

Valid field spec

var a = r.WithField("Phone","$.[\'Contact.Phone.Value\']", isArray: false);

Sample fiddle: https://dotnetfiddle.net/B1BkFi

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flattening a JSONObject in Java - Recursion causing ...
I'm using Iterator to loop the keys, and using hasNext() and next() to ensure that I should only be able to access specific...
Read more >
Flattening JSON objects in Python
There are many ways to flatten JSON. There is one recursive way and another by using the json-flatten library. Approach 1: Recursive Approach....
Read more >
Question about flatten and unflatten #1989 - nlohmann/json
IMHO, we can change the array to object when we get [json.exception.parse_error.109] parse error: array index 't' is not a number .
Read more >
How to Flatten or Unflatten Complex JSON objects into Flat ...
Couple of days back I got a questions on how to flatten JSON Object which may be simple of Complex in structure? JsonFlattener...
Read more >
Flattening JSON objects in Python
Flattening JSON objects involves converting complex hierarchical JSON structures to simpler structures. This process is often required when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found